2

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

我的布局 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 代码中,我尝试扩展上述布局并添加一个带有按钮的线性布局。

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

但是 newLinear 视图(及其子视图)没有显示出来。

4

3 回答 3

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% 工作。

于 2012-10-12T04:18:27.220 回答
0

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

像这样。

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

于 2012-10-12T03:45:44.377 回答
0

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

注释行

设置内容视图(主布局);

于 2012-10-12T03:52:43.380 回答