我有需求,我需要结合以编程方式生成的布局和xml。我应该采取什么方法?请让我知道。谢谢你。
生成布局:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout audio = new LinearLayout(this);
audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);
RecordButton = new RecordButton(this);
audio.addView(RecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
PlayButton = new PlayButton(this);
audio.addView(PlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
setContentView(audio);
}
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="top">
<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />
创建一个容器视图来存放视图,并将生成的视图添加到其中,
这是xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="top">
<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />
<!-- create a container view to hold your view -->
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/next" />
</RelativeLayout>
在你的代码中,
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.the_above_layout);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LinearLayout audio = new LinearLayout(this);
audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);
RecordButton = new RecordButton(this);
audio.addView(RecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
PlayButton = new PlayButton(this);
audio.addView(PlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
// add the view to your container
container.addView(audio);
}