簡體   English   中英

以編程方式將單選按鈕文本置於按鈕頂部

[英]Radio button text to top of the button programmatically

有沒有辦法以編程方式將單選按鈕的文本對齊到頂部,如下所示。

在此處輸入圖片說明

我使用以下代碼創建廣播組

  final RadioButton[] rb = new RadioButton[5];
            RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
            rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
            rg.setLayoutParams(radioparams);

            for (int i = 0; i < 5; i++) {
                rb[i] = new RadioButton(getActivity());
                rb[i].setText("Radiobtn " + i);
                rb[i].setId(i + 100);
                rg.addView(rb[i]);

            }
            layout.addView(rg);

但是我將文本顯示在每個按鈕的右側。

    final RadioButton[] rb = new RadioButton[5];
    RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
    rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
    rg.setLayoutParams(radioparams);

    for (int i = 0; i < 5; i++) {
        rb[i] = new RadioButton(getActivity());
        rb[i].setText("Radiobtn " + i);
        rb[i].setId(i + 100);
        rb[i].setButtonDrawable(null);

        TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {android.R.attr.listChoiceIndicatorSingle});
        int attributeResourceId = a.getResourceId(0, 0);
        Drawable drawable = getResources().getDrawable(attributeResourceId);

        rb[i].setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
        rb[i].setGravity(Gravity.CENTER | Gravity.BOTTOM);
        rg.addView(rb[i]);

    }
    layout.addView(rg);

嘗試這種方式可以技巧

使您的布局像這樣

<LinearLayout
    android:id="@+id/lnrView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:orientation="horizontal">


</LinearLayout>

而不是像這樣以編程方式添加RadioButton

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            LinearLayout layout = new LinearLayout(MainActivity.this);

            layout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);


            LinearLayout layout2 = new LinearLayout(MainActivity.this);
            layout2.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);


            final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            final TextView textView = new TextView(MainActivity.this);
            int id = 0;
            textView.setId(id);
            textView.setTextSize(14);
            textView.setTextColor(Color.rgb(0, 0, 0));
            textView.setMaxEms(2);
            textView.setText("NIlu");
            layout2.addView(textView);

            RadioButton radioButton = new RadioButton(MainActivity.this);
            layout2.addView(radioButton);
            layout.setLayoutParams(lp);

            lnrView.addView(layout2);


        }
    });

暫無
暫無

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

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