开发者

Relative layout with Button and LinearLayout, contents of LL is rendered horizontally not vertically with embedded answer

I have below RelativeLayout with a Button and LinearLayout. I am adding TextViews to LinearLayout but even though I have set orientation to vertical in LinearLayout attribute the content of LinearLayout is coming horizontal.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layou开发者_如何学JAVAt_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>

And programatically I am doing like this:-

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);

Any clue where I am wrong?

Answer:

Adding below line worked:

h.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


Eventhough the LinearLayout orientation is set to vertical, I was getting horizontal rendering of listview. The reasons was text views are added to linearlayout dynamically, hence textview parameters need to set programatically. Below I have mentioned how I have achieved this.

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));


<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜