是否可以复制我在Java中用XML创建的线性布局?
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/divider1"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Principal"
android:textColor="@color/Black" />
<Spinner
android:id="@+id/principal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="3dp"
android:alpha="0.8"
android:entries="@array/Principal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Item"
android:textColor="@color/Black" />
<Spinner
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="3dp"
android:alpha="0.8"
android:entries="@array/Product" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Quantity"
android:textColor="@color/Black" />
<Spinner
android:id="@+id/qty"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="3dp"
android:alpha="0.8"
android:entries="@array/Quantity" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Item Code"
android:textColor="@color/Black" />
<EditText
android:id="@+id/itemCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:alpha="0.8"
android:background="@drawable/edittext"
android:textColor="@color/Black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Remarks per order"
android:textColor="@color/Black" />
<EditText
android:id="@+id/rmrksPO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:alpha="0.8"
android:background="@drawable/edittext"
android:textColor="@color/Black" />
</LinearLayout>
我希望在单击下面的TextView时,我希望复制的对象的线性布局低于现有布局。
<LinearLayout
android:id="@+id/ll3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/divider2"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/addNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/divider2"
android:text="@string/Add_NEw"
android:textStyle="bold" />
</LinearLayout>
答案 0 :(得分:0)
您可以夸大布局:
LayoutInflater inflater = LayoutInflater.from(context);
View yourLayout = inflater.inflate(yourLayoutId, yourParent, false);
&#34; yourLayout&#34;将是xml中布局的一个实例。
答案 1 :(得分:0)
// Add Linear Layout Run time to Parent Layout, On the click of Textview
public class DuplicateLayout extends Activity {
TextView addNew;
LinearLayout ll3,LinearLayoutMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duplicate_layout);
LinearLayoutMain = (LinearLayout)findViewById(R.id.LinearLayoutMain);
ll3 = (LinearLayout)findViewById(R.id.ll3);
addNew = (TextView)findViewById(R.id.addNew);
addNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater)getSystemService(DuplicateLayout.LAYOUT_INFLATER_SERVICE);
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.activity_duplicate_layout, null);
LinearLayoutMain.addView(child, 1);
}
});
}
}
//activity_duplicate_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayoutMain"
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffff"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/ll3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/addNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/divider2"
android:text="Add_NEw"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>