动态(以编程方式)创建视图时,视图在卡片视图中重叠
Views overlap in card view when creating views dynamically (programmatically)
我想在布局中按下按钮时在卡片视图中动态创建两个编辑文本字段。当我尝试这个时,两个编辑文本视图在同一个地方相互重叠,我尝试了一些方法,但我仍然不知道如何解决这个问题。
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>
当前输出现在看起来像这样:
试试这个方法。问题是 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 );
}
});
}
}
我想在布局中按下按钮时在卡片视图中动态创建两个编辑文本字段。当我尝试这个时,两个编辑文本视图在同一个地方相互重叠,我尝试了一些方法,但我仍然不知道如何解决这个问题。
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>
当前输出现在看起来像这样:
试试这个方法。问题是 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 );
}
});
}
}