Android 未在适当位置创建动态视图。重叠上一视图。安卓
我是Android新手,所以在过去的几天里我一直在努力调整,但我被困在这一部分。对于我的应用程序,我试图输入一个按钮“添加更多”,该按钮可以放大视图以帮助合并用户输入,但我遇到了一些问题。我环顾四周,利用他的帮助解决了我的问题,但我遇到了一个问题,我膨胀的xml布局与以前的设置布局重叠 这是我为新的\u类\u容器文件设置的布局Android 未在适当位置创建动态视图。重叠上一视图。安卓,android,android-layout,android-activity,android-edittext,android-view,Android,Android Layout,Android Activity,Android Edittext,Android View,我是Android新手,所以在过去的几天里我一直在努力调整,但我被困在这一部分。对于我的应用程序,我试图输入一个按钮“添加更多”,该按钮可以放大视图以帮助合并用户输入,但我遇到了一些问题。我环顾四周,利用他的帮助解决了我的问题,但我遇到了一个问题,我膨胀的xml布局与以前的设置布局重叠 这是我为新的\u类\u容器文件设置的布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="@drawable/gradient_gray">
<!-- Class Name -->
<TextView
android:id="@+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write_class_name"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="@+id/register_class_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/register_class_textview"
android:layout_alignBaseline="@+id/register_class_textview"
android:inputType="text"
android:hint="@string/example_name"
android:singleLine="true" >
</EditText>
<!-- Assignment Type and Grade Weight -->
<TextView
android:id="@+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_class_textview"
android:layout_alignLeft="@+id/register_class_textview"
android:text="@string/write_assignment_type"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_assign_textview"
android:layout_alignLeft="@+id/register_assign_textview"
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true">
</EditText>
<!-- Save Button -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom" >
<Button
android:id="@+id/button_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/save_class" />
</RelativeLayout>
<Button
android:id="@+id/btnAddNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_assign_edit"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="@string/more_assign"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnAddNewItem"
android:layout_alignLeft="@+id/register_weight_textview"
android:ems="10"
android:hint="@string/example_weight"
android:inputType="number"
android:singleLine="true" >
</EditText>
<TextView
android:id="@+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/register_assign_textview"
android:layout_alignBottom="@+id/register_assign_textview"
android:layout_alignParentRight="true"
android:text="@string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我能做些什么来防止它重叠并显示在下面?我怎样才能在每次“更新”时移动按钮?因为如果我将按钮移动到新的_类xml文件,那么最初添加更多视图时它将不会显示
我尝试实现下面的代码,它所做的是使我能够在第一次正确地添加动态视图,但每次都是在它与添加的视图重叠之后
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.register_assign_edit);
rowView.setLayoutParams(p);
我不确定,但您可以尝试按照代码移动任何视图。我建议像主布局一样使用RelativeLayout
RelativeLayout.LayoutParams bParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bParams1.leftMargin = (int) leftMrgn;
bParams1.topMargin = (int) topMgrn;
bParams1.height = (int) bHeight;
bParams1.width = (int) bWidth;
anyView.setLayoutParams(bParams1);
RelativeLayout
s的行为类似于FrameLayout
,如果您不给它们任何LayoutParams
(下面、上面、中间等约束)
您可以构造适当的
RelativeLayout.LayoutParams
,或者在第一个布局中创建一个空的垂直LinearLayout
,该布局将包含行。然后,只需将行添加到线性布局
而不是相对布局
,这将使用两个布局文件:main\u layout
和layout\u to\u include
main_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/sv1" >
<LinearLayout
android:id="@+id/llAddingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="@layout/layout_to_include"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/llButtonHolder"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/sv1"
android:gravity="center_horizontal"
android:layout_above="@+id/button2" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Add More" />
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:text="Save Class" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Ex: CS 301" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/linearLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Assignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: Homework" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: 10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
很多代码。。。很多文字。。。简言之单击按钮时是否要向当前布局添加内容?是吗?@MateuszZając我要单击按钮,使其使用视图更新活动。在这种情况下,当用户按下“添加更多”按钮时,会显示额外的TextView和EditView。我在上面链接了一张图片,展示了它应该是什么样子。你想要的是在布局方面很容易做到,但很难管理。获取并跟踪所有输入将很困难。@vikram您能建议布局的方法吗?当然。我已经贴出了答案。看看它是否符合你的要求。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/sv1" >
<LinearLayout
android:id="@+id/llAddingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="@layout/layout_to_include"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/llButtonHolder"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/sv1"
android:gravity="center_horizontal"
android:layout_above="@+id/button2" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Add More" />
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:text="Save Class" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Ex: CS 301" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/linearLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Assignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: Homework" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: 10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
....
setContentView(R.layout.main_layout);
....
llAddingContainer = (LinearLayout) findViewById(R.id.llAddingContainer);
// Clicking on this button will add another view from 'layout_to_include' to the ScrollView
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_to_include, null);
// Find the elements in this view and set them up
EditText et1 = (EditText) view.findViewById(R.id.editText1)
....
....
....
// Add the view to the LinearLayout inside ScrollView
llAddingContainer.addView(view);
});