【发布时间】:2017-03-28 23:48:09
【问题描述】:
解决了下面的代码
如果我的布局类似于以下布局:
这五个临时 EditTexts 代表用户可以输入的一些信息(例如商品的价格、订单号等)。如果用户想要添加另一个商品,他们会单击“添加”按钮,而我想要另一个5 个文本视图出现在屏幕上两个按钮的正上方,但在前一组 5 EditTexts 的正下方。有人可以给我一个关于我将如何做到这一点的起点。
我的片段布局是这样的:
- 我有一个顶级线性布局(垂直方向)。
- 然后是滚动视图。
- 在滚动视图中,我有另一个线性布局。
- 在线性布局中,我有这五个 EditText 对象和两个按钮
上面的视图是一个片段(在下面的文件中定义),我在我的 MainActivity 文件中传递给我的 FragmentAdapter:
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添加它 -
哦,好吧,您是说使用
LayoutInflater将其添加到scrollview中? -
@1290 试试这个把那些按钮放在滚动视图之外可能会粘在屏幕底部。因此,当您点击 ADD 时,继续将 textview 添加到该滚动视图中,您现在是如何做到的
-
@Raghavendra 这就是我两秒钟前所做的,哈哈!我将这些按钮放在了相对布局中的滚动视图之外(我将在上面更新我的布局文件)。现在,当我点击添加时,我需要一种方法将这五个文本视图打包成线性布局并将其放入滚动视图中。
-
是的。制作一个包含 5 个
EditTexts的模板 xml,然后使用LayoutInflater在单击按钮或类似操作时将它们添加到视图中。