如何在Java中将单选按钮(Radiobutton)的文本设置在图像下方

huangapple go评论77阅读模式
英文:

How to set Radiobutton's text below image in Java

问题

我的问题非常简单。我目前有类似这样的单选按钮:
如何在Java中将单选按钮(Radiobutton)的文本设置在图像下方

我想将文本设置为图像下方(并在按钮被点击时将其设置为粗体)。

我的 XML 文件如下:

<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioGroup
            android:id="@+id/radio_group_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"/>

</HorizontalScrollView>

我 Java 代码的一部分如下:

RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);

for(int i=0; i<categories.size(); i++) {
    final String name = categories.get(i).name;
    final int resource = categories.get(i).resource;

    final RadioButton button = new RadioButton(getContext());
    button.setButtonDrawable(resource);
    button.setText(name);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 设置文本为粗体(我还会添加一些逻辑)
        }
    });

    categoryGroup.addView(button);
}
英文:

My question is fairly simple. I currently have Radiobuttons that look like this :
如何在Java中将单选按钮(Radiobutton)的文本设置在图像下方

I would like to set the text to be below the image (and eventually set it as bold when the button is clicked).

My XML file looks like this :

&lt;HorizontalScrollView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;

        &lt;RadioGroup
            android:id=&quot;@+id/radio_group_category&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;/&gt;

&lt;/HorizontalScrollView&gt;

And part of my Java code :

RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);

        for(int i=0; i&lt;categories.size(); i++) {
            final String name = categories.get(i).name;
            final int resource = categories.get(i).resource;

            final RadioButton button = new RadioButton(getContext());
            button.setButtonDrawable(resource);
            button.setText(name);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Set text as bold (I&#39;ll add some logic too)
                }
            });

            categoryGroup.addView(button);
        }

答案1

得分: 0

尝试一下这个代码:

public class MainActivity extends AppCompatActivity {

    LinearLayout linearLayout;
    RadioGroup radioGroup;
    RadioButton radioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);

        linearLayout = new LinearLayout(getApplicationContext());
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        radioGroup = new RadioGroup(getApplicationContext());
        linearLayout.addView(radioGroup);

        for (int i = 0; i < 5; i++) {
            radioButton = new RadioButton(getApplicationContext());
            radioButton.setText(String.valueOf(i));
            radioGroup.addView(radioButton);
        }

        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params1.setMargins(3, 0, 3, 5);
        getWindow().addContentView(linearLayout, params1);
    }
}

这段代码对我有效,看看这个效果:

这就是它的外观:
如何在Java中将单选按钮(Radiobutton)的文本设置在图像下方

英文:

Try this

 public class MainActivity extends AppCompatActivity {

LinearLayout linearLayout;
RadioGroup radioGroup;
RadioButton radioButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // setContentView(R.layout.activity_main);

    linearLayout = new LinearLayout(getApplicationContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    radioGroup = new RadioGroup(getApplicationContext());
    linearLayout.addView(radioGroup);

    for (int i = 0; i&lt;5 ;i++){
        radioButton = new RadioButton(getApplicationContext());
        radioButton.setText(String.valueOf(i));
        radioGroup.addView(radioButton);
    }



    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params1.setMargins(3, 0, 3, 5);
    getWindow().addContentView(linearLayout, params1);


  }
}

This worked for me check this out

this is how it looks.
如何在Java中将单选按钮(Radiobutton)的文本设置在图像下方

答案2

得分: 0

尝试这个:

RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);

for (int i = 0; i < categories.size(); i++) {
    final String name = categories.get(i).name;
    final int resource = categories.get(i).resource;

    final RadioButton button = new RadioButton(getContext());
    Drawable drawable = getResources().getDrawable(resource);
    button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    button.setButtonDrawable(null);
    button.setText(name);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                button.setText(Html.fromHtml("<h2>" + button.getText().toString() + "</h2>",
                        Html.FROM_HTML_MODE_COMPACT));
            } else {
                button.setText(Html.fromHtml("<h2>" + button.getText().toString() + "</h2>"));
            }
        }
    });

    categoryGroup.addView(button);
}
英文:

Try this :

RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);

        for(int i=0; i&lt;categories.size(); i++) {
            final String name = categories.get(i).name;
            final int resource = categories.get(i).resource;

            final RadioButton button = new RadioButton(getContext());
            Drawable drawable = getResources().getDrawable(resource);
            button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            button.setButtonDrawable(null);
            button.setText(name);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
                        button.setText(Html.fromHtml(&quot;&lt;h2&gt;&quot;+button.getText().toString()+&quot;&lt;/h2&gt;&quot;,
                                Html.FROM_HTML_MODE_COMPACT));
                    } else {
                        button.setText(Html.fromHtml(&quot;&lt;h2&gt;&quot;+button.getText().toString()+&quot;&lt;/h2&gt;&quot;));
                    }
                }
            });

            categoryGroup.addView(button);
        }

huangapple
  • 本文由 发表于 2020年10月22日 17:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64479185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定