我正在尝试做这样的底层菜单:



我以这种方式定义了布局:

<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));

最佳答案

您不需要使用iflater,只要获得Drawable resurse就足够了:

android.graphics.drawable.Drawable shape = getResources().getDrawable(R.drawable.item_panel_borde);


如果您在其他视图中使用此drawable资源,则可能会遇到麻烦,并且可以使用android.graphics.drawable.LayerDrawable(Drawable [])构造函数以编程方式创建LayerList。

10-07 19:49
查看更多