我在drawable文件夹中为拇指图像创建了xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="5px"
android:left="5px"
android:right="5px"
android:top="5px">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<gradient
android:endColor="#4DB9E6"
android:startColor="#4DB9E6" />
<size
android:height="40dp"
android:width="40dp" />
</shape>
</item>
</layer-list>
运作良好。
我想要不同大小的拇指图像xml。
如何动态实现这一目标?
答案 0 :(得分:2)
您可以通过访问
来更改drawable的大小getResources().getDrawable(R.drawable.thumb_xml);
之后使用: setBound()方法。
但您也可以通过代码和更改方法参数在运行时创建Drawable:
Drawable getThumb(int width,int height){
GradientDrawable thumb= new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {Color.parseColor("#4DB9E6"),
Color.parseColor("#4DB9E6")});
thumb.setShape(GradientDrawable.OVAL);
thumb.setStroke(2,Color.parseColor("#FFFFFF"));
thumb.setBounds(0, 0, 40, 40);
return thumb;
}
答案 1 :(得分:0)
尝试删除
<size
android:width="60dp"
android:height="40dp" />
或者如果您希望以编程方式更改大小,请尝试以下代码
Drawable drawable = getResources().getDrawable(R.drawable.your_file_name);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5),
(int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
答案 2 :(得分:0)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/topSpace"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/leftSpace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/ImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/rightSpace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/bottomSpace"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>