android - 添加一个填充屏幕高度的视图

时间:2013-12-29 16:15:19

标签: android fill-parent

我想以编程方式将View添加到mainHolder,声明如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >

<LinearLayout
    android:id="@+id/mainHolder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="horizontal" />

我希望新视图填满屏幕高度。怎么可能?

2 个答案:

答案 0 :(得分:1)

试试这个:

LinearLayout mainHolder = (LinearLayout)findViewById(R.id.mainHolder);
//untested adding of view I got it from http://stackoverflow.com/questions/5731487/how-to-add-views-to-linear-layout
mainHolder.addView(View, mainHolder);
//this is tested however
View.setMinimumHeight(mainHolder.getHeight());

答案 1 :(得分:0)

设置线性布局属性     机器人:layout_width = “match_content”     机器人:layout_height = “match_content”

然后添加以下代码:         LinearLayout mainHolder =(LinearLayout)findViewById(R.id.mainHolder);

    RelativeLayout.LayoutParams layer=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
            RelativeLayout.LayoutParams.MATCH_PARENT);
    layer.addRule(RelativeLayout.ALIGN_PARENT_TOP);


    mainHolder.addView(layer);

与上述代码相同,您可以以编程方式添加视图