简体   繁体   中英

how to put the button on the right of the android java layout

I have a linear layout display in android java like this, I want the button to be located on the right side of the CheckBox as in the picture I want. I tried for days but I failed and I'm really asking for help

here is the code

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);
            }
        }); }

and this is the result

the result

This is where I want the views:

i want

You can achieve this quite easily. Just put the check box and button in a relative layout container:

<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>

Here you go. You need to add a Vertical Layout per row.

for (int i = 0; i < gejala.length; i++) 
{ 
llh = new LinearLayout(this); 
llH.setOrientation(LinearLayout.HORIZONTAL); 
lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
llh.setLayoutParams(lp);
.....
llh.addView(cbgejala[i]); //view button
...
llh.addView(btnview[i]);
...
ll.addView(llh);
...
}

Try this code:

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);
        }
    }); }

What this does is put the button and text in a horizontal linear layout before inserting that horizontal linear layout into the vertical one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM