我想知道如何创建将线性布局添加到其他布局的方法。我想创建一个方法,当我在活动中使用此方法时,此方法会在我的活动布局顶部添加线性布局。我怎样才能做到这一点?
编辑:我做这样的事情:
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>
但我的布局并没有改变他的颜色。为什么?