我有一个需求,需要将通过编程生成的布局与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" />
some_view_in_xml.addView(generated_layout);
这个怎么样? - K Neeraj Lalaudio.xml.addView(generated)
吗?我尝试了一下,但它说无法解析 XML。 - Iman Yasmin