动态创建视图时(编程方式)视图在卡片视图中重叠

问题描述 投票:1回答:1

我想在布局中按下按钮时在卡片视图中动态创建两个编辑文本字段。当我尝试这个时,两个编辑文本视图在同一个地方重叠,我尝试了一些方法,但我仍然不知道解决这个问题。

Java代码:

public class MainActivity extends AppCompatActivity {
private Context mContext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final FloatingActionButton button = (FloatingActionButton) findViewById(R.id.fab);
    button.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @SuppressLint("SetTextI18n")
        public void onClick(View v) {

            RelativeLayout rl = (RelativeLayout) findViewById(R.id.li);
            mContext = getApplicationContext();



            //card view
            CardView card = new CardView(mContext);
            // Set the CardView layoutParams
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            card.setLayoutParams(params);

            // Set CardView corner radius
            card.setRadius(9);
            // Set cardView content padding
            card.setContentPadding(15, 50, 15, 50);
            // Set a background color for CardView
            card.setCardBackgroundColor(Color.parseColor("#ffffff"));
            // Set the CardView maximum elevation
            card.setMaxCardElevation(15);
            // Set CardView elevation
            card.setCardElevation(9);

            RelativeLayout layout = new RelativeLayout(MainActivity.this);

            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


            // Add edittext1
            EditText et1 = new EditText(MainActivity.this);
            et1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            et1.setHint("Your Question");

            et1.setId(View.generateViewId());
            //textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
            //textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
            card.addView(et1,params1);

            // Add edittext 2
            EditText et2 = new EditText(MainActivity.this);
            et2.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            et2.setHint("Your Answer");
            params2.addRule(RelativeLayout.BELOW, et1.getId());
            card.addView(et2,params2);

            //card view into linearlayout
            rl.addView(card );


        }
    });

}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_anchorGravity="bottom|right|end"
    android:layout_marginStart="350dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    app:srcCompat="@android:drawable/ic_input_add"
    android:layout_marginLeft="350dp" />

<RelativeLayout
    android:id="@+id/li"
    android:layout_marginTop="10dp"
    android:layout_margin="20dp"
    android:layout_marginStart="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp">

</RelativeLayout>

</RelativeLayout>

现在的输出看起来像这样:

enter image description here

java android android-cardview layoutparams
1个回答
1
投票

试试这种方式。问题是CardView从Framelayout扩展,因此当您将视图直接添加到卡视图视图重叠时。请注意,我已将et1和et2添加到布局,然后布局到Cardview。

public class MainActivity extends AppCompatActivity {
private Context mContext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final FloatingActionButton button = (FloatingActionButton) findViewById(R.id.fab);
    button.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @SuppressLint("SetTextI18n")
        public void onClick(View v) {

            RelativeLayout rl = (RelativeLayout) findViewById(R.id.li);
            mContext = getApplicationContext();



            //card view
            CardView card = new CardView(mContext);
            // Set the CardView layoutParams
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            card.setLayoutParams(params);

            // Set CardView corner radius
            card.setRadius(9);
            // Set cardView content padding
            card.setContentPadding(15, 50, 15, 50);
            // Set a background color for CardView
            card.setCardBackgroundColor(Color.parseColor("#ffffff"));
            // Set the CardView maximum elevation
            card.setMaxCardElevation(15);
            // Set CardView elevation
            card.setCardElevation(9);

            RelativeLayout layout = new RelativeLayout(MainActivity.this);

            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


            // Add edittext1
            EditText et1 = new EditText(MainActivity.this);
            et1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            et1.setHint("Your Question");

            et1.setId(View.generateViewId());
            //textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
            //textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
            layout.addView(et1,params1);

            // Add edittext 2
            EditText et2 = new EditText(MainActivity.this);
            et2.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            et2.setHint("Your Answer");
            params2.addRule(RelativeLayout.BELOW, et1.getId());
            layout.addView(et2,params2);

            card.addView(layout)
            rl.addView(card );


        }
    });

}

}
© www.soinside.com 2019 - 2024. All rights reserved.