【发布时间】:2013-12-29 19:01:33
【问题描述】:
我正在尝试做一个像这样的底层菜单:
我是这样定义布局的:
<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 行:错误 膨胀类层列表
所以我认为这不是正确的方法。有没有办法代替每个菜单项定义一个 XML 文件?
谢谢!
----------- 编辑 -----------
我找到了一个“肮脏”的解决方法来实现我的目标:将其中一个按钮的背景作为 LayerDrawable 并将位图替换为正确的。这不是我想要的方式(因为我希望它完全以编程方式)也不是最初的问题,所以我不会回答自己让问题打开希望有人能提供帮助,但这就是我的意思现在做:
我为 XML 文件中的相应“项目”分配了一个 id:
<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));
【问题讨论】:
标签: java android xml android-layout