android:将视图膨胀为布局



我在一个布局中添加了一个由layout充气器充气的视图,然后我将该视图的背景颜色更改为蓝色,例如红色。之后,我在同一布局中添加一个用相同xml文件充气的新视图,但我得到的视图背景颜色为蓝色,而不是原始颜色为红色。这是测试代码:

public class InflateActivity extends Activity{

    private LinearLayout mContainer;
    private View view;
    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.inflate_test);
        mContainer = (LinearLayout)findViewById(R.id.inflate_container);
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
                mContainer.addView(view,2);
            }
        });
        findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setBackgroundColor(Color.BLUE);
            }
        });
    }
} 

通过视图.toString(),我得到了不同的id。我不明白为什么。我想知道为什么我对视图A进行了一些更改,而使用未更改的同一xml文件膨胀的视图B将受到这些更改的影响。实际上,我对视图A做了很多更改,但只有setBackgroundColor()会影响视图B。xml文件的根视图是FrameLayout。其他布局(如LinearLayout、RelativeLayout)不会出现问题

我通过创建一个项目检查了您的代码。我使用的是安卓工作室。我没有发现你提到的任何问题。只要看看我的代码,然后尝试相应地更改你的代码。

主要活动:

public class MainActivity extends Activity {

private LinearLayout mContainer;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContainer =  (LinearLayout)findViewById(R.id.inflate_container);
    findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.expanded_record,mContainer,false);
                mContainer.addView(view,mContainer.getChildCount());
            }
        });
        findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setBackgroundColor(Color.BLUE);
            }
        });
}

主要XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"
android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"   tools:context=".MainActivity">
<LinearLayout
    android:id="@+id/inflate_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp">

    <TextView
        android:text="add" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/add"
        android:clickable="true"/>
    <TextView
        android:id="@+id/change_color"
        android:clickable="true"
        android:text="change_color" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

以及我正在夸大的XML

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="#000000"
android:layout_gravity="center">

</View>

这是我得到的结果

看到我可以添加黑色视图并将其更改为蓝色,然后再次添加黑色

http://postimg.org/image/ggeotcz91/

更改视图后。setBackgroundColor(Color.BLUE);背景将是蓝色的。如果你想在添加新视图时将其更改为红色,你需要更改

findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setBackgroundColor(Color.BLUE);//changes this view which clicked to blue
        }
    });

根据您上面提供的代码片段,因为您在添加时使用了相同的膨胀视图,所以当您添加它时,第二次视图的背景是蓝色的。如果您想添加具有所有默认值的视图作为您正在使用的布局"expanded_record.xml"的视图,那么您必须在第二次添加时再次对其进行膨胀。简而言之,您应该使用默认条件对"expanded_record.xml"进行扩展,您可以根据需要对每个实例进行更改。

 view1 = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
 mContainer.addView(view1,2);
 view2 = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
 mContainer.addView(view2,3);

最新更新