addContentView和layoutInflater混合和混合的内容 [英] addContentView and layoutInflater mix and blend the contents
问题描述
我有两个单独的XML文件,如下图所示,两者都放在布局文件夹内。其中一个被命名为 firstlayout
,另一个是 secondlayout
。在活动中,我想说明同时使用的内容 layoutinflater
。尽管,在 firstlayout.xml
其内容导向,水平对齐和 secondlayout.xml
有一个 RelativeLayout的
的中心对齐,但是,当我使用 addcontentview
,输出是一个水平都放置在屏幕的顶部的共混文本
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.
我希望在屏幕上输出的是,在中心处的顶部和水平文字水平文本。请检查我的文件和低于code,让我知道我错过了什么。
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:
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
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:
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>
推荐答案
您所设置的LayoutParams到WRAP_CONTENT第二布局,所以即使你设置内的RelativeLayout为中心的引力,你的外在的LinearLayout仍在继续在左上角和大小自身对准包裹的内容,所以一切都将在左上结束。
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.
卸下secondlayout.xml的LinearLayout中,你不需要它。然后改变你的code为:
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);
这篇关于addContentView和layoutInflater混合和混合的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!