英文:
How to set Radiobutton's text below image in Java
问题
我想将文本设置为图像下方(并在按钮被点击时将其设置为粗体)。
我的 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 :
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 :
<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>
And part of my Java code :
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) {
//Set text as bold (I'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);
}
}
这段代码对我有效,看看这个效果:
英文:
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<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
答案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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论