我试图像这样做一个底层菜单:
我已经用这种方式定义了布局:
<LinearLayout
android:id="@+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- First item of the menu -->
<ImageButton
android:id="@+id/BConex1"
android:contentDescription="@string/desc_gen_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:layout_gravity="left"
android:background="@drawable/menu_bottom"
android:layout_marginLeft="4dp" />
<!-- Second item of the menu -->
<ImageButton
android:id="@+id/BConex2"
android:contentDescription="@string/desc_gen_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:layout_gravity="left"
android:background="@drawable/menu_bottom"
android:layout_marginLeft="4dp" />
<!-- ... Some additional items in the menu -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
@ drawable / menu_bottom是一个图层列表,以这种方式定义:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1px"
android:color="#55ffffff" />
<padding
android:left="16px"
android:top="6px"
android:right="16px"
android:bottom="6px" />
<solid
android:color="#cc000000" />
<gradient
android:startColor="#222222"
android:centerColor="#111111"
android:endColor="#000000"
android:angle="-90" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item>
<bitmap
android:src="@+drawable/bitmap_to_change"
android:tileMode="disabled"
android:width="30dp"
android:height="15dp"
android:gravity="center" />
</item>
</layer-list>
现在的问题是:除了位图的 src 字段之外,我想对菜单中的每个按钮使用完全相同的背景。我尝试在其中一个按钮上使用LayoutInflater并尝试以编程方式更改它,如下所示:
View first = LayoutInflater.from(this).inflate(R.drawable.menu_bottom, null);
first.findViewById(R.drawable.bitmap_to_change).setBackground(this.getResources().getDrawable(R.drawable.another_resource));
但是这会返回一个异常:
12-11 11:35:53.556:E / AndroidRuntime(897):java.lang.RuntimeException: 无法启动活动ComponentInfo {XXX.MainActivity}: android.view.InflateException:二进制XML文件行#2:错误 inflating class layer-list
所以我认为这不是正确的方法。有没有办法实现这一点,而不是为每个菜单项定义一个XML文件?
谢谢!
-----------编辑-----------
我发现了一个&#34;脏&#34;解决方法以实现我的目标:将其中一个按钮的背景作为LayerDrawable并用正确的替换Bitmap。这不是我想要这样做的方式(因为我想要完全以程序方式编写),也不是原来的问题,所以我不会回答自己让问题打开,希望有人可以提供帮助,但这就是我现在正在做的事情:
我为相应的&#34;项目&#34;分配了一个ID在XML文件中:
<item android:id="@+id:bitmap_layer">
以编程方式:
ImageButton firstButton = (ImageButton) findViewById(R.id.BConex1);
LayerDrawable bg = (LayerDrawable) firstButton.getBackground();
bg.setDrawableByLayerId(R.id.bitmap_layer, getResources().getDrawable(R.drawable.the_new_drawable));
答案 0 :(得分:4)
你不需要使用iflater,获得Drawable resurse就足够了:
android.graphics.drawable.Drawable shape = getResources().getDrawable(R.drawable.item_panel_borde);
使用您的workaroud,如果在其他视图中使用这个可绘制资源,可能会遇到麻烦,您可以使用android.graphics.drawable.LayerDrawable(Drawable [])构造函数以编程方式创建LayerList。
答案 1 :(得分:0)
你期待像iPhone风格标签。 然后查看此帖子Custom tab look and feel of IPhone in android