I add a view which is inflated by LayoutInflater into a layout, then I change the background color of this view,for example, red to blue .After all that, I add a new view , inflated with the same xml file, into the same layout,but i got a view with the background color blue, not the original color red. Here is the test code:
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);
}
});
}
}
And I got different id by view.toString(). I can not figure out why. I wonder why I make some changes to view A,and the view B which is inflate with the same xml file that not be changed will be affected by these changes. Actually,I've made many changes to the view A, but only setBackgroundColor() will affect view B. The root view of the xml file is a FrameLayout.Other layout like LinearLayout,RelativeLayout will not come up the problem
I checked your code by creating a project. I am using Android Studio. I did not find any of that problem that you mentioned. Just have a look at my code and try to change yours accordingly.
MainActivity:
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);
}
});
}
Main 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>
and the XML that I am inflating
<?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>
This is the result I got
See that I can add black views and change them to blue and then again black
http://postimg.org/image/ggeotcz91/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With