在我班上我想在开始位置设置无线电组按钮但是没有正确放置。这是我的班级观点:
layout.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="@+id/buttonp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/buttons"
android:text="more question" />
<Button
android:id="@+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Submit" />
<TextView
android:id="@+id/tvTimeCount"
style="@style/normalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="00:00" />
</RelativeLayout>
活动代码:
public class Rate_me_up extends Activity implements OnClickListener,
OnCheckedChangeListener {
RelativeLayout layout;
int id = 1;
private TextView textViewShowTime;
private long totalTimeCountInMilliseconds = 60 * 1 / 4 * 1000;
private long timeBlinkInMilliseconds = 30 * 1000 * 1 / 4;
private CountDownTimer countDownTimer;
private boolean blink;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_me_up);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
layout = (RelativeLayout) findViewById(R.id.linear);
Button btnp = (Button) findViewById(R.id.buttonp);
btnp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
radioGroup.setOrientation(0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layout.addView(radioGroup, p);
RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
radioButtonView.setText("radio1");
radioButtonView.setId(id++);
radioButtonView.setButtonDrawable(R.drawable.e404);
radioButtonView.setChecked(false);
radioGroup.addView(radioButtonView, p);
RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
radioButtonView2.setText("radio2");
radioButtonView2.setId(id++);
radioButtonView2.setChecked(false);
radioGroup.addView(radioButtonView2, p);
RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
radioButtonView3.setText("radio3");
radioButtonView3.setId(id++);
radioGroup.addView(radioButtonView3, p);
radioButtonView3.setChecked(false);
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
// layout.addView(txt, p);
radioGroup.setOnCheckedChangeListener(this);
}
答案 0 :(得分:3)
由于您已经定义了RelativeLayout Params,因此您只需向其添加规则即可将RadioGroup与顶部对齐。
p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
之前这样做:
layout.addView(radioGroup, p);
修改强>
您还需要将RelativeLayout.LayoutParams中的高度更改为WRAP_CONTENT。
编辑2:
如果您想要将无线电组动态添加到彼此之下,您可以这样做:
首先添加两个实例变量
private RadioGroup mLastRadioGroup;
private int mRadioGroupId = 1111; //just a default start id.
然后在分配LayoutParams后立即添加此行。 (如果你想要动态添加,请抛弃上面提到的规则)
if (mLastRadioGroup != null)
p.addRule(RelativeLayout.BELOW, mLastRadioGroup.getId());
然后在将radioGroup添加到布局之前,设置其ID。并将mLastRadioGroup分配给您的广播组。
radioGroup.setId(mRadioGroupId++);
mLastRadioGroup = radioGroup;
layout.addView(radioGroup, p); // I moved this to the bottom
这样我们就知道了最后一组ID,我们可以继续在它下面添加一个新的。
答案 1 :(得分:0)
我认为这是因为您的xml文件RelativeLayout
中第二个android:layout_height="150dp"
的高度需要设置为android:layout_height="fill_parent"