问题
I have two separate xml files as shown below and both of them are placed inside the layout folder. One of the is named firstlayout
and the other is secondlayout
. In the activity, I want to show the contents of both using layoutinflater
. Despite of, the firstlayout.xml
its contents are oriented and aligned horizontally and the secondlayout.xml
has a relativelayout
aligned at the center, however, when I use addcontentview
as shown below in my code, the output is a horizontal blended text of both placed at the top of the screen.
I expected the output on the screen to be, horizontal text at the top and horizontal text at the center. Please check my files and the code below and let me know what i am missing.
Java_Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstlayout);
LayoutInflater loiViewInflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
loiViewInflater = LayoutInflater.from(getApplicationContext());
View mView = loiViewInflater.inflate(R.layout.secondlayout, null);
addContentView(mView, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first xml file00"
android:gravity="top">
</TextView>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first xml file01"
android:layout_marginLeft="18dp"
android:gravity="top">
</TextView>
</LinearLayout>
secondlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/RelativeLayout00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second xml file00">
</TextView>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second xml file01"
android:layout_toRightOf="@+id/TextView00">
</TextView>
</RelativeLayout>
回答1:
You're setting the LayoutParams to WRAP_CONTENT for the second layout, so even though you set the gravity of the inner RelativeLayout to center, your outer LinearLayout is still going to align at the top left and size itself to wrap the content, so everything will end up in the top left.
Remove the LinearLayout from secondlayout.xml, you don't need it. Then change your code to:
View mView = loiViewInflater.inflate(R.layout.secondlayout, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
addContentView(mView, params);
来源:https://stackoverflow.com/questions/23664432/addcontentview-and-layoutinflater-mix-and-blend-the-contents