android:将视图添加到膨胀的视图中

时间:2012-10-12 03:40:56

标签: android android-linearlayout layout-inflater

我想从res / layout目录中扩展一个布局,并在我的activity类中添加一些视图。

我的布局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_marginTop="30dip"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/reg_button"
        style="@style/simple_button"
        android:onClick="clickButton"
        android:text="@string/reg_button" />


    <!--  I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

在我的java代码中,我尝试对上面的布局进行膨胀,并使用Button添加linearLayout。

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    //inflate xml
    LinearLayout mainLayout = (LinearLayout)     LayoutInflater.from(getBaseContext()).inflate(R.layout.regions_search, null);


    //Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);        
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    //create  a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    //first , I add button to the LinearLayout
    newLinear.addView(test);

    //Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);


    //display
    setContentView(mainLayout);

但是新的线性视图(及其子视图)没有显示出来。

3 个答案:

答案 0 :(得分:3)

在您的活动中使用此功能

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // display
    setContentView(R.layout.activity_main);

    Context mContext = getApplicationContext();
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.regions_search);
    // Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    // create a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    // first , I add button to the LinearLayout
    newLinear.addView(test);

    // Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);
}

并在您的xml文件(activity_main.xml)中写入,如下所示,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regions_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dip" 
android:orientation="vertical">

<Button
    android:id="@+id/reg_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="clickButton"
    android:text="RegButton" />

<!-- I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

试试这个它将100%工作。

答案 1 :(得分:0)

您可以尝试为已添加的新LinearLayoutButton添加宽度和高度参数吗?创建视图时需要这些参数。

像这样。

newLinear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

答案 2 :(得分:0)

test.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1f));

评论专栏

的setContentView(mainLayout);

相关问题