【问题标题】:Change background Drawable padding dynamically动态更改背景可绘制填充
【发布时间】:2013-11-18 18:48:18
【问题描述】:

我想创建一个接收百分比并仅动态显示这部分背景的视图,作为参数。

例如使用参数 0.8 创建视图时;背景仅在视图宽度的 80% 处绘制(左侧填充 0%,右侧填充 20%)。

我尝试创建一个 DrawableGradient 作为 View 背景,但它与整个背景重叠,我无法调整此背景的大小。

我的下一个选择是创建 InsetDrawable 或 Layer-List 就像这个答案:Android drawable with background and gradient on the left,但我无法动态更改填充。

我目前使用的例子: res/layout/acitvity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
tools:context=".MainActivity" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_80"
    android:text="80%" 
    android:textSize="16sp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_20"
    android:text="20%"
    android:textSize="16sp"/>

</LinearLayout>

可绘制/percentage_background_20.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:bottom="5dp"
        android:drawable="@drawable/background"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"/>
    <item
        android:bottom="5dp"
        android:drawable="@drawable/view_background"
        android:left="5dp"
        android:right="200dp"
        android:top="5dp"/>

</layer-list>

可绘制/percentage_background_80.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="5dp"
        android:drawable="@drawable/background"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"/>
    <item
        android:bottom="5dp"
        android:drawable="@drawable/view_background"
        android:left="5dp"
        android:right="50dp"
        android:top="5dp"/>
</layer-list>

drawable/background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@android:color/white" />
</shape>

drawable/view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item><shape>
            <gradient
                android:angle="0"
                android:startColor="@android:color/holo_blue_dark"
                android:endColor="@android:color/white"
                android:type="linear" />
        </shape>
   </item>
</selector>

这是它的外观,但我无法从源代码更改列表中的填充值。你知道我该如何实现吗?

澄清: 在 drawable/percentage_background_80.xml 和 drawable/percentage_background_20.xml 中有元素:

android:right="XXXdp"

我想以编程方式更改 XXXdp 的值。

【问题讨论】:

  • 你为什么不使用 ClipDrawable ?
  • 嗯,ClipDrawable 几乎是我所需要的,但它会从两侧夹住 Drawable。我不能只从右侧剪辑它。
  • 在 ClipDrawable 构造函数中使用适当的重力
  • 哦,是的,你是对的。但是还有一个问题,因为 ClipDrawable 去掉了背景 drawable 的一部分,所以 GradientDrawable 在这里没有用。你知道其他可以调整其子级而不是剪切它的 Drawable 吗?
  • ScaleDrawable?我建议阅读一些关于 Drawable 类的文档

标签: android android-layout user-interface


【解决方案1】:

需要添加到:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_80"

    android:padding="your_padding"

    android:text="80%" 
    android:textSize="16sp"/>

这应该有效。对于动态,请阅读:

——how to manually set textview padding?

——Adding Margins to a dynamic Android TextView

编辑:

另外,您可以将 TextView 添加到布局并更改背景,如下所示:

<LinearLayout      
  android:id=@"+id/your_id"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/percentage_background_80"
        android:text="80%" 
        android:textSize="16sp"/>

</LinearLayout>

在您的班级中,只需使用方法 findViewById 并设置背景。

【讨论】:

  • 这会为 TextView 添加填充,因此长文本不会匹配 TextView 的整个宽度。我只想改变背景。
  • @Benjamin,'所以长文本不会匹配整个宽度'是什么意思?您也可以为此目的使用 paddingTop、paddingBottom。
  • 我想创建如上所示的背景,而不是在 TextView 中创建填充。我只想更改背景可绘制对象,而不更改 TextView 视图本身。您的提议更改了 TextView 填充,因此内部也将被填充。
  • 为什么不对 TextView 使用任何布局容器并仅更改背景可绘制对象?
【解决方案2】:

使用 ClipDrawable 或 ScaleDrawable 取决于您只是想剪辑目标 Drawable 还是调整它的大小

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多