我想在布局中按下按钮时在卡片视图内动态创建两个编辑文本字段。尝试此操作时,两个编辑文本视图在同一位置彼此重叠,我尝试了一些方法,但我仍然不知道如何解决此问题。
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>
当前输出如下所示:
答案 0 :(得分:1)
尝试这种方式。问题是CardView是从Framelayout扩展而来的,因此,当您直接将视图添加到Card 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 );
}
});
}
}