Android 带有按钮和线性布局的相对布局,LL的内容以水平方式呈现,而不是以嵌入答案的方式垂直呈现
我在下面有一个按钮和线性布局。我正在向LinearLayout添加文本视图,但即使我在LinearLayout属性中将方向设置为垂直,LinearLayout的内容仍然是水平的Android 带有按钮和线性布局的相对布局,LL的内容以水平方式呈现,而不是以嵌入答案的方式垂直呈现,android,android-layout,Android,Android Layout,我在下面有一个按钮和线性布局。我正在向LinearLayout添加文本视图,但即使我在LinearLayout属性中将方向设置为垂直,LinearLayout的内容仍然是水平的 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:text="Add a server" android:id="@+id/addHost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addHost"/>
<LinearLayout android:orientation="vertical"
android:id="@+id/listhosts"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/addHost" android:layout_weight="1">
</LinearLayout>
</RelativeLayout>
知道我错在哪里吗
答复:
在下面的行中添加:
h、 setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容)) 尽管LinearLayout方向设置为垂直,但我得到的是listview的水平渲染。原因是文本视图是动态添加到linearlayout的,因此需要通过编程设置文本视图参数。下面我已经提到了我是如何做到这一点的
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.listhosts);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView h = new TextView(this);
h.setText(line);
h.setId(index++);
linearLayout.addView(h);
//Adding below line worked for me.
h.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
您好,devA,我已经编辑了xml文件,因为所有内容都包含在TableLayout中。将其更改为LinearLayout会导致程序在模拟器上崩溃,或将表格行包装到LinearLayout中会导致模拟器强制退出。您好,devA,我已将布局更改为RelativeLayout,但问题仍然存在。你会建议在这里也包装线性布局吗?嗯,我正在向线性布局添加textview,如下所示。TextView h=新的TextView(此);h、 setText(行);h、 setId(index++);线性布局。添加视图(h);这有什么问题吗?所有的内容都是水平线而不是垂直线?我们必须在文本视图中添加新行吗?我被卡住了欢迎任何人的帮助。我终于得到了答案。在下面的行中添加是有效的。h、 setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容));感谢德瓦,感谢你的投入。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/listhosts"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow android:baselineAligned="false">
<Button android:text="Add a server" android:id="@+id/addHost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addHost"/>
</TableRow>
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:id="@+id/listhosts1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
</TableRow>
</LinearLayout>
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.listhosts);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView h = new TextView(this);
h.setText(line);
h.setId(index++);
linearLayout.addView(h);
//Adding below line worked for me.
h.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));