簡體   English   中英

動態(以編程方式)創建視圖時,視圖在卡片視圖中重疊

[英]Views overlap in card view when creating views dynamically (programmatically)

當在布局中按下按鈕時,我想在卡片視圖中動態創建兩個編輯文本字段。 當我嘗試這個時,兩個編輯文本視圖在同一個地方相互重疊,我嘗試了一些方法,但我仍然不知道如何解決這個問題。

爪哇代碼:

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>

當前輸出現在看起來像這樣:

在此處輸入圖片說明

試試這個方法。 問題是 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 );


        }
    });

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM