如何将按钮放在android java布局的右侧

时间:2018-12-10 20:31:32

标签: java android android-layout

我在android java中有一个线性布局显示,就像我想要的那样,我希望按钮位于CheckBox的右侧,如图所示。 我尝试了几天,但失败了 我真的在寻求帮助

这是代码

public void Tampilan() {
    Display display = getWindowManager().getDefaultDisplay();

    ScrollView sv = (ScrollView) findViewById(R.id.svAnalisa); // new ScrollView(this);
    //ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    //sv.setLayoutParams(lp);
    LinearLayout l = new LinearLayout(this);
    l.setOrientation(LinearLayout.VERTICAL);
    sv.addView(l);
    HorizontalScrollView hv = new HorizontalScrollView(this);
    ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    hv.setLayoutParams(lp);
    l.addView(hv);

    //LinearLayout ll = new LinearLayout(this);
    ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setMinimumWidth(300);

    //newcheckbox
    cbgejala = new CheckBox[gejala.length];
    //newbuttton
    btnview = new Button[gejala.length];

    for (int i = 0; i < gejala.length; i++) {

        //view checkbox
        cbgejala[i] = new CheckBox(this);
        cbgejala[i].setText(gejala[i]);
        ll.addView(cbgejala[i]);

        //view button
        btnview[i] = new Button(this);
        btnview[i].setText("Detail");
        btnview[i].setNextFocusRightId(cbgejala[i].getRight());
        btnview[i].setRight(2);
        btnview[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        ll.addView(btnview[i]);

        //onclick btnview
        final int finalI = i;
        btnview[i].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent b = new Intent(AnalisaActivity.this, detailGejala.class);
                b.putExtra("namagejala", gejala[finalI]);
                b.putExtra("keterangangejala", keterangan_gejala[finalI]);
                startActivity(b);
            }
        }); }

这就是结果

the result

这是我想要的视图:

i want

3 个答案:

答案 0 :(得分:0)

您可以轻松实现这一目标。 只需将复选框和按钮放在相对的布局容器中即可:

<RelativeLayout
...
android:width = "match_parent"
>

<CheckBox
       android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:gravity="start"/>


 <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:gravity="end"
            />
</RelativeLayout>

答案 1 :(得分:0)

您在这里。您需要为每行添加一个垂直布局。

"2018-09-10 22:01:00".toDate().formatTo("dd MMM yyyy")

答案 2 :(得分:0)

尝试以下代码:

public void Tampilan() {
Display display = getWindowManager().getDefaultDisplay();

ScrollView sv = (ScrollView) findViewById(R.id.svAnalisa); // new ScrollView(this);
//ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//sv.setLayoutParams(lp);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
HorizontalScrollView hv = new HorizontalScrollView(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
hv.setLayoutParams(lp);
l.addView(hv);

//LinearLayout ll = new LinearLayout(this);
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setMinimumWidth(300);

//newcheckbox
cbgejala = new CheckBox[gejala.length];
//newbuttton
btnview = new Button[gejala.length];

for (int i = 0; i < gejala.length; i++) {

    LinearLayout hl = new LinearLayout();
    hl.setOrientation(LinearLayout.VERTICAL);
    ViewGroup.LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    hl.setLayoutParams(p);

    //view checkbox
    cbgejala[i] = new CheckBox(this);
    cbgejala[i].setText(gejala[i]);
    hl.addView(cbgejala[i]);

    //view button
    btnview[i] = new Button(this);
    btnview[i].setText("Detail");
    btnview[i].setNextFocusRightId(cbgejala[i].getRight());
    btnview[i].setRight(2);
    btnview[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    hl.addView(btnview[i]);

    ll.addView(hl);

    //onclick btnview
    final int finalI = i;
    btnview[i].setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent b = new Intent(AnalisaActivity.this, detailGejala.class);
            b.putExtra("namagejala", gejala[finalI]);
            b.putExtra("keterangangejala", keterangan_gejala[finalI]);
            startActivity(b);
        }
    }); }

这是将按钮和文本放置在水平线性布局中,然后再将该水平线性布局插入垂直布局中的方法。