【发布时间】:2013-05-02 19:01:00
【问题描述】:
我正在尝试根据用户实时指定的输入为我的 Android 应用程序实现动态按钮膨胀。单击时,按钮的颜色会从蓝色变为红色。
负责这个的代码如下:
LayoutInflater layoutsInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_for_buttons);
// Let's say that now we need only 3 buttons:
for (int i = 0; i < 3; i++) {
RelativeLayout buttonLayout = (RelativeLayout) layoutsInflater
.inflate(R.layout.button_layout, linearLayout, false);
Button button = (Button) buttonLayout.getChildAt(0);
button.setText("Button " + i);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View buttonView) {
Button button = (Button) buttonView;
GradientDrawable gradientDrawable = (GradientDrawable) button
.getBackground();
gradientDrawable.setColor(Color.RED);
gradientDrawable.invalidateSelf();
}
});
linearLayout.addView(buttonLayout);
linearLayout.invalidate();
}
我附加按钮的布局是一个简单的LinearLayout:
<LinearLayout
android:id="@+id/layout_for_buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
膨胀的button_layout 是由Button 和TextView 组成的RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp" >
<Button
android:id="@+id/button_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
android:gravity="left"
android:paddingBottom="7dip"
android:paddingLeft="50sp"
android:paddingTop="7dip"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/label_view"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="13dip"
android:layout_marginTop="8dip"
android:gravity="center"
android:text="Hi"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
@drawable/button_bg 是蓝色的shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#0000ff" />
<corners android:radius="10dp" />
</shape>
现在,当我运行应用程序时,一切似乎都很好。被点击后,第一个按钮不出所料地变成红色:
当我关闭并重新运行应用程序时,每个按钮都是蓝色的,这符合我的预期。问题是,当我第二次重新运行应用程序时,每个按钮都会变成红色(就像被点击一样):
正如文档中所写的那样,inflate 方法应该根据给定的 XML 扩充新的视图层次结构,那么这可能是什么原因呢?我认为相同的GradientDrawable 可能会在每个膨胀按钮之间共享,但调试器显示每个按钮都有自己的GradientDrawable 实例。
【问题讨论】:
标签: android android-view layout-inflater