字段重叠,以编程方式创建布局

时间:2018-07-02 17:47:48

标签: android

我有一个布局XML,当我单击一个浮动按钮时,我会以编程方式创建一个布局,以便将该布局的数据传递给这个新布局。

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundnewlayout"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:background="@drawable/img_interna" />

        <TextView
            android:id="@+id/txtTitle_avaliacao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="Medicamento"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/txtTitle_avaliacao"
            android:layout_marginBottom="40dp"
            android:fillViewport="true">

            <RelativeLayout
                android:id="@+id/linearCardView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <android.support.v7.widget.CardView
                    android:id="@+id/cardView"
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    android:layout_margin="5dp"
                    android:elevation="7dp"
                    android:minHeight="200dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/txtNome_medicamento"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginTop="10dp"
                            android:alpha="0.54"
                            android:text="Nome:"
                            android:textSize="10sp" />

                        <AutoCompleteTextView
                            android:id="@+id/edtNome_medicamento"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/txtNome_medicamento"
                            android:layout_marginStart="10dp"
                            android:backgroundTint="@color/White"
                            android:hint="Paracetamol"
                            android:maxLines="1"
                            android:padding="7dp"
                            android:textColor="@color/black"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtQtd_medicamento"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/edtNome_medicamento"
                            android:layout_marginStart="10dp"
                            android:layout_marginTop="10dp"
                            android:alpha="0.54"
                            android:text="Quantidade:"
                            android:textSize="10sp" />

                        <EditText
                            android:id="@+id/edtQtd_medicamento"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/txtQtd_medicamento"
                            android:layout_marginStart="10dp"
                            android:backgroundTint="@color/White"
                            android:hint="3"
                            android:maxLines="1"
                            android:minWidth="50dp"
                            android:padding="7dp"
                            android:textColor="@color/black"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtPosologia_medicamento"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/edtQtd_medicamento"
                            android:layout_marginStart="10dp"
                            android:layout_marginTop="10dp"
                            android:alpha="0.54"
                            android:text="Posologia:"
                            android:textSize="10sp" />

                        <EditText
                            android:id="@+id/edtPosologia_medicamento"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/txtPosologia_medicamento"
                            android:layout_marginStart="10dp"
                            android:backgroundTint="@color/White"
                            android:hint="2 vezes ao dia a cada 8 horas"
                            android:minWidth="50dp"
                            android:padding="7dp"
                            android:textColor="@color/black"
                            android:textSize="14sp" />

                    </RelativeLayout>
                </android.support.v7.widget.CardView>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fltNewLayout"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/cardView"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="170dp"
                    android:backgroundTint="@color/colorPrimaryDark"
                    android:clickable="true"
                    android:src="@drawable/icone_check"
                    app:fab_colorPressed="@color/colorPrimary"
                    app:rippleColor="@android:color/white" />

                <LinearLayout
                    android:id="@+id/linearReceita"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/fltNewLayout"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:orientation="vertical">

                </LinearLayout>
            </RelativeLayout>
        </ScrollView>
    </FrameLayout>
</RelativeLayout>

当我单击浮动按钮时,将使用固定的此布局数据创建一个新布局。

但是,以编程方式创建的布局字段重叠,如何解决?

以下方法如下:

private void createNewLayout(){
    linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(5,15,5,5);
    //linearLayout.setLayoutParams(params);
    cardView = new CardView(getContext());
    cardView.setLayoutParams(linearLayout.getLayoutParams());
    cardView.setPadding(10,10,10,10);
    cardView.setMaxCardElevation(6);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cardView.setElevation(6);
    }
    TextView txtNomeTitle = new TextView(getContext());
    txtNomeTitle.setText("Nome");
    txtNomeTitle.setLayoutParams(linearLayout.getLayoutParams());
    cardView.addView(txtNomeTitle);
    TextView txtNomeMedicamento = new TextView(getContext());
    txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
    edtNomeMed1.setText("");
    txtNomeMedicamento.setLayoutParams(params);

    cardView.addView(txtNomeMedicamento);
    linearLayout.addView(cardView);
}

1 个答案:

答案 0 :(得分:0)

我通过添加新的线性布局解决了该问题,该线性布局垂直添加了文本视图,在添加了这些字段之后,将其插入了cardview。

/**
 * New added Linearlayout
 */
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);

更改以下代码:

 private void createNewLayout() {
        linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 0, 5, 5);

        cardView = new CardView(getContext());
        cardView.setLayoutParams(params);
        //cardView.setPadding(10, 10, 10, 10);
        cardView.setMaxCardElevation(6);
        /**
         * New added Linearlayout
         */
        LinearLayout linearCard = new LinearLayout(getContext());
        linearCard.setOrientation(LinearLayout.VERTICAL);

        cardView.addView(linearCard);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cardView.setElevation(6);
        }
        TextView txtNomeTitle = new TextView(getContext());
        txtNomeTitle.setText("Nome");
        txtNomeTitle.setLayoutParams(params);
        //cardView.addView(txtNomeTitle);
        linearCard.addView(txtNomeTitle);
        TextView txtNomeMedicamento = new TextView(getContext());
        txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
        edtNomeMed1.setText("");
        txtNomeMedicamento.setLayoutParams(params);
        txtNomeMedicamento.setPadding(10, 10, 10, 10);
        // cardView.addView(txtNomeMedicamento);
        linearCard.addView(txtNomeMedicamento);
        //cardView.addView(linearLayout);
        linearLayout.addView(cardView);
    }

我将字段直接插入cardview中,而内部没有布局,这使字段重叠。