0

我想知道如何创建将线性布局添加到其他布局的方法。我想创建一个方法,当我在活动中使用此方法时,此方法会在我的活动布局顶部添加线性布局。我怎样才能做到这一点?

编辑:我做这样的事情:

public class SecondActivity extends Activity {

    LinearLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        layout = (LinearLayout) findViewById(R.id.lay);
        Button next = (Button) findViewById(R.id.nextActivity);
        layout.addView(addNewLinearLayout(getApplicationContext()));

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        });
    }
    private View addNewLinearLayout(Context context) {
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
        // Do something else here on your linear layout or to customize your linear layout
        return linearLayout;
    }

}

这是xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/nextActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_marginTop="100dp"
        />

</LinearLayout>

但我的布局并没有改变他的颜色。为什么?

4

4 回答 4

2

尝试这样的事情:

private View _addNewLinearLayout(context) {
    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(params);
    // Do something else here on your linear layout or to customize your linear layout

    return linearLayout;
}

主要你可以这样称呼:

getView().addView(_addNewLinearLayout(context));
于 2013-02-06T08:00:22.327 回答
1

您可以像这样轻松地创建一个新的 LinearLayout:

LinearLayout linLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);

然后将其设置为:

setContentView(linLayout, layoutParams);
于 2013-02-06T07:59:29.860 回答
1

您使用此示例方法。如果需要,您可以添加更多参数。

public LinearLayout addLinearLayout(Context context) {
    LinearLayout newLayout = new LinearLayout(context);

    //Add stuffs here, like LayoutParams

    return newLayout;
}

yourLinearLayoutName.addView(addLinearLayout(yourClassName.this));
于 2013-02-06T08:00:29.073 回答
1
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View detailView = inflater.inflate(R.layout.yourNewLayout, l_details, false);

其中 l_details 是您要在其中添加另一个线性布局的线性布局的实例。

于 2013-02-06T08:01:19.880 回答