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

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

How to set Radiobutton's text below image in Java

问题

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

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

我的 XML 文件如下:

  1. <HorizontalScrollView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content">
  4. <RadioGroup
  5. android:id="@+id/radio_group_category"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:orientation="horizontal"/>
  9. </HorizontalScrollView>

我 Java 代码的一部分如下:

  1. RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
  2. for(int i=0; i<categories.size(); i++) {
  3. final String name = categories.get(i).name;
  4. final int resource = categories.get(i).resource;
  5. final RadioButton button = new RadioButton(getContext());
  6. button.setButtonDrawable(resource);
  7. button.setText(name);
  8. button.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View view) {
  11. // 设置文本为粗体(我还会添加一些逻辑)
  12. }
  13. });
  14. categoryGroup.addView(button);
  15. }
英文:

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 :

  1. &lt;HorizontalScrollView
  2. android:layout_width=&quot;wrap_content&quot;
  3. android:layout_height=&quot;wrap_content&quot;&gt;
  4. &lt;RadioGroup
  5. android:id=&quot;@+id/radio_group_category&quot;
  6. android:layout_width=&quot;wrap_content&quot;
  7. android:layout_height=&quot;wrap_content&quot;
  8. android:orientation=&quot;horizontal&quot;/&gt;
  9. &lt;/HorizontalScrollView&gt;

And part of my Java code :

  1. RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
  2. for(int i=0; i&lt;categories.size(); i++) {
  3. final String name = categories.get(i).name;
  4. final int resource = categories.get(i).resource;
  5. final RadioButton button = new RadioButton(getContext());
  6. button.setButtonDrawable(resource);
  7. button.setText(name);
  8. button.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View view) {
  11. //Set text as bold (I&#39;ll add some logic too)
  12. }
  13. });
  14. categoryGroup.addView(button);
  15. }

答案1

得分: 0

尝试一下这个代码:

  1. public class MainActivity extends AppCompatActivity {
  2. LinearLayout linearLayout;
  3. RadioGroup radioGroup;
  4. RadioButton radioButton;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. // setContentView(R.layout.activity_main);
  9. linearLayout = new LinearLayout(getApplicationContext());
  10. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
  11. LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  12. radioGroup = new RadioGroup(getApplicationContext());
  13. linearLayout.addView(radioGroup);
  14. for (int i = 0; i < 5; i++) {
  15. radioButton = new RadioButton(getApplicationContext());
  16. radioButton.setText(String.valueOf(i));
  17. radioGroup.addView(radioButton);
  18. }
  19. LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
  20. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  21. params1.setMargins(3, 0, 3, 5);
  22. getWindow().addContentView(linearLayout, params1);
  23. }
  24. }

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

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

英文:

Try this

  1. public class MainActivity extends AppCompatActivity {
  2. LinearLayout linearLayout;
  3. RadioGroup radioGroup;
  4. RadioButton radioButton;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. // setContentView(R.layout.activity_main);
  9. linearLayout = new LinearLayout(getApplicationContext());
  10. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
  11. LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  12. radioGroup = new RadioGroup(getApplicationContext());
  13. linearLayout.addView(radioGroup);
  14. for (int i = 0; i&lt;5 ;i++){
  15. radioButton = new RadioButton(getApplicationContext());
  16. radioButton.setText(String.valueOf(i));
  17. radioGroup.addView(radioButton);
  18. }
  19. LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  20. params1.setMargins(3, 0, 3, 5);
  21. getWindow().addContentView(linearLayout, params1);
  22. }
  23. }

This worked for me check this out

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

答案2

得分: 0

尝试这个:

  1. RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
  2. for (int i = 0; i < categories.size(); i++) {
  3. final String name = categories.get(i).name;
  4. final int resource = categories.get(i).resource;
  5. final RadioButton button = new RadioButton(getContext());
  6. Drawable drawable = getResources().getDrawable(resource);
  7. button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
  8. button.setButtonDrawable(null);
  9. button.setText(name);
  10. button.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  14. button.setText(Html.fromHtml("<h2>" + button.getText().toString() + "</h2>",
  15. Html.FROM_HTML_MODE_COMPACT));
  16. } else {
  17. button.setText(Html.fromHtml("<h2>" + button.getText().toString() + "</h2>"));
  18. }
  19. }
  20. });
  21. categoryGroup.addView(button);
  22. }
英文:

Try this :

  1. RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
  2. for(int i=0; i&lt;categories.size(); i++) {
  3. final String name = categories.get(i).name;
  4. final int resource = categories.get(i).resource;
  5. final RadioButton button = new RadioButton(getContext());
  6. Drawable drawable = getResources().getDrawable(resource);
  7. button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
  8. button.setButtonDrawable(null);
  9. button.setText(name);
  10. button.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
  14. button.setText(Html.fromHtml(&quot;&lt;h2&gt;&quot;+button.getText().toString()+&quot;&lt;/h2&gt;&quot;,
  15. Html.FROM_HTML_MODE_COMPACT));
  16. } else {
  17. button.setText(Html.fromHtml(&quot;&lt;h2&gt;&quot;+button.getText().toString()+&quot;&lt;/h2&gt;&quot;));
  18. }
  19. }
  20. });
  21. categoryGroup.addView(button);
  22. }

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:

确定