簡體   English   中英

如何將按鈕放在android java布局的右側

[英]how to put the button on the right of the android java 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);
            }
        }); }

這就是結果

結果

這是我想要的視圖:

我想要

您可以輕松實現這一目標。 只需將復選框和按鈕放在相對的布局容器中:

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

干得好。 您需要為每行添加一個垂直布局。

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

試試這個代碼:

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

這樣做是在將按鈕和文本插入水平的線性布局之前,將按鈕和文本放置在水平的線性布局中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM