【发布时间】:2014-02-16 17:30:56
【问题描述】:
我想将表格行动态添加到我的布局中,这些行将添加到名为 main_ScrollView_Container
的 RelativeLayout我遇到的问题是:
- 添加的行按添加顺序堆叠在一起,而不是彼此下方。
- 如何检索添加的行,以便可以读取/写入 EditText input 和 TextView output 我添加的每一行?
我的创建:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
// the inflater
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// the item to inflate
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.main_ScrollView_Container);
// the item to inflate with
View tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
relativeLayout.addView(tableRow, 0);
tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
relativeLayout.addView(tableRow, 1);
tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
relativeLayout.addView(tableRow, 2);
tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
relativeLayout.addView(tableRow, 3);
// retrieve/set values to the EditText input
// retrieve/set values to the TextView output
}
我得到了这个 my_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" >
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:gravity="center"
android:hint="@string/input"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="@string/output"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
和我的布局
<ScrollView
android:id="@+id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/main_ScrollView_Container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</ScrollView>
【问题讨论】:
标签: android layout dynamic view row