2

我是 Android 新手,所以过去几天我一直在尝试调整,但被困在这一部分。对于我的应用程序,我正在尝试输入一个“添加更多”按钮,它会放大视图以帮助合并用户输入,但我遇到了问题。我环顾四周,使用他的源代码来帮助解决我的问题,但我遇到了一个问题,即我的膨胀 xml 布局与我之前的设置布局重叠。

这是我为 new_class_container 文件设置的布局

    <?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>

就是上面代码的布局的样子,我希望能够按下“添加更多”按钮,它会膨胀以“创建新行”,例如,分配类型和等级权重的 TextViews 和 EditTexts各自的TextViews。但是当我运行应用程序并按添加更多时,新的一组 TextView + EditText 会弹出我所拥有的顶部而不是底部。以下是我的另一个xml文件new_class

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
             android:id="@+id/register_assign_textview"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
             android:text="@string/write_assignment_type"
             android:textAppearance="?android:attr/textAppearanceMedium"/>

        <TextView 
             android:id="@+id/register_weight_textview"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
             android:text="@string/write_assign_percent"
             android:textAppearance="?android:attr/textAppearanceMedium"
             android:layout_marginLeft="30dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
             android:id="@+id/register_assign_edit"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:gravity="center_horizontal"
             android:hint="@string/example_assign"
             android:singleLine="true">
         </EditText>

        <EditText
             android:id="@+id/register_weight_edit"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:hint="@string/example_weight"
             android:layout_marginLeft="30dp"
             android:gravity="center_horizontal"
             android:inputType="number"
             android:singleLine="true">
         </EditText>

    </LinearLayout>
</LinearLayout>

这是我拥有的 Java 代码

public class NewClass extends Activity{

    private RelativeLayout mContainerView;

    private Button mAddButton;

    private View mExclusiveEmptyView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_class_container);
        mContainerView = (RelativeLayout) findViewById(R.id.parentView);
        mAddButton = (Button) findViewById(R.id.btnAddNewItem);
    }

    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        //TODO: Handle Screen Rotation
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // TODO: Handle screen rotation:
        // restore the saved items and inflate each one with inflateEditRow;

    }

    // onClick handler for the "Add new" button;
    public void onAddNewClicked(View v) {
        // Inflate a new row and hide the button self.
        inflateEditRow(null);
        v.setVisibility(View.GONE);
    }

    // onClick handler for the "X" button of each row
    public void onDeleteClicked(View v) {
        // remove the row by calling the getParent on button
        mContainerView.removeView((View) v.getParent());
    }

    private void inflateEditRow(String name) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.new_class, null);
        //final ImageButton deleteButton = (ImageButton) rowView
            //  .findViewById(R.id.buttonDelete);
        final EditText editText = (EditText) rowView
                .findViewById(R.id.register_assign_edit);

        if (name != null && !name.isEmpty()) {
            editText.setText(name);
        } else {
            mExclusiveEmptyView = rowView;
            //deleteButton.setVisibility(View.INVISIBLE);
        }

        // A TextWatcher to control the visibility of the "Add new" button and
        // handle the exclusive empty view.
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().isEmpty()) {
                    mAddButton.setVisibility(View.GONE);
                    //deleteButton.setVisibility(View.INVISIBLE);

                    if (mExclusiveEmptyView != null
                            && mExclusiveEmptyView != rowView) {
                        mContainerView.removeView(mExclusiveEmptyView);
                    }
                    mExclusiveEmptyView = rowView;
                } else {

                    if (mExclusiveEmptyView == rowView) {
                        mExclusiveEmptyView = null;
                    }

                    mAddButton.setVisibility(View.VISIBLE);
                //  deleteButton.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }
        });

        // Inflate at the end of all rows but before the "Add new" button
        mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
    }
}

我能做些什么来防止它重叠并出现在下面?每次“更新”时,我如何才能移动按钮?因为如果我将按钮移动到 new_class 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);
4

3 回答 3

0

这将适用于有两个布局文件:main_layoutlayout_to_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>

layout_to_include.xml

<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);
    });
于 2013-07-29T08:21:12.273 回答
0

我不确定,但您可以尝试按照代码移动任何视图。我建议像主布局一样使用 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);
于 2013-07-27T23:00:03.160 回答
0

RelativeLayoutFrameLayout如果你不给他们任何东西LayoutParams(像下面、上面、中心等的约束),s 的行为就像 a 。

您可以构建适当的,或者更好的是,在您的第一个布局中创建一个包含行RelativeLayout.LayoutParams的空垂直。LinearLayout然后,只需将行添加到LinearLayout而不是RelativeLayout.

于 2013-07-28T00:42:39.653 回答