gpt4 book ai didi

java - 从 XML 膨胀可绘制对象

转载 作者:行者123 更新时间:2023-11-30 06:21:25 25 4
gpt4 key购买 nike

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

enter image description here

我是这样定义布局的:

<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: Unable to start activity ComponentInfo{XXX.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class layer-list

所以我认为这不是正确的方法。有没有一种方法可以实现这一点,而不是为每个菜单项定义一个 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);

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

关于java - 从 XML 膨胀可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20525350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com