java - 动态(以编程方式)创建 View 时, View 在卡片 View 中重叠

标签 java android android-cardview layoutparams

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

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

最佳答案

试试这个方法。问题是 CardView 从 Framelayout 扩展,因此当您直接将 View 添加到卡片 View 时, View 会重叠。请注意,我已将 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 );


        }
    });

}

}

关于java - 动态(以编程方式)创建 View 时, View 在卡片 View 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880121/

相关文章:

java - 当我在列表中使用这些图像的 Uris 时,如何使用图库使用 Intent 查看外部存储中的多个图像

java - 我想知道java中一行的含义

java - 将 jar 导入 android 项目时出错

Android 绘图库 - achartengine 还是 Androidplot?

android - CardView 海拔不适用于 RecyclerView

android - CardView 的 OnClickListener 不工作 android?

带有嵌套对象的 Java8 流

java - 高性能的JDBC批量查询

java - 使用android从MySql数据库中删除用户

android - 如何将 CardView 属性添加到应用主题?