解決下面的代碼後,顯示更多的意見WORKS安卓:如何點擊一個按鈕
如果我有類似下面的一個佈局:
這五個臨時EditTexts
代表用戶可以輸入的一些信息(如項目的價格,訂單號碼等)。如果用戶想要添加另一個項目,他們將點擊添加按鈕,並且我希望在屏幕右上方的屏幕上出現另外5個文本視圖兩個按鈕,但在前一組下方的5 EditTexts
。有人能給我一個關於如何做到這一點的起點。
我片段的佈局是這樣的:
- 我有一個頂層的線性佈局(垂直方向)。
- 然後滾動查看。
- 在scrollview中我有另一個線性佈局。
- 在該線性佈局我有這五個的EditText目的和兩個按鈕
上面的視圖是一個片段(下面在文件中定義),其餘傳遞給我FragmentAdapter在我MainActivity文件:
public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}
下面是該片段我的佈局文件:
<LinearLayout 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:id="@+id/device_fragment_linear_layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />
<Button
android:id="@+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
edit_text_view_objects.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>
</LinearLayout>
當我動態地添加新的意見,我做什麼,我想添加,然後通過添加它的模板'LayoutInflater' – Aidin
哦確定,所以你說使用' LayoutInflater'將其添加到'scrollview'中? – CapturedTree
@ 1290試試把這些按鈕放在scrollview的外面,可能會粘在屏幕的底部。所以,當你點擊ADD繼續添加textview到那個滾動視圖如何你現在已經這麼做 – Raghavendra