如何在android中以编程方式创建按钮左侧带有文本的单选按钮?
我创建了单选按钮,按钮左侧的文本为xml:如何在android中以编程方式创建按钮左侧带有文本的单选按钮?,android,Android,我创建了单选按钮,按钮左侧的文本为xml: <RadioButton android:id="@+id/radio_sattelite1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:button="@null" android:checked="true"
<RadioButton
android:id="@+id/radio_sattelite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="@null"
android:checked="true"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="center_vertical"
android:onClick="onRadioButtonClicked"
android:text="@string/sattelite1" />
但现在的问题是左右两侧的按钮都出现了 试试这个
yourradiobutton.setCompoundDrawables(drawable_left, drawable_top, drawable_right, drawable_bottom);
编辑
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setCompoundDrawables(null, null, drawable, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setButtonDrawable(android.R.color.transparent);
radioButton.setBackgroundDrawable(null);
试试这个
yourradiobutton.setCompoundDrawables(drawable_left, drawable_top, drawable_right, drawable_bottom);
编辑
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setCompoundDrawables(null, null, drawable, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setButtonDrawable(android.R.color.transparent);
radioButton.setBackgroundDrawable(null);
试试这种方法,希望这能帮助你解决问题
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
radioButton.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_launcher), null, null, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
试试这种方法,希望这能帮助你解决问题
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
radioButton.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_launcher), null, null, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
不向单选按钮添加文本,您可以做一件事,在具有水平方向的线性布局中,首先添加一个文本视图,然后在该添加单选按钮旁边添加一个不带文本的文本视图,如下所示:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" text="button text" >
</TextView>
<RadioButton
android:id="@+id/radio_sattelite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="onRadioButtonClicked"/>
</LinearLayout>
不向单选按钮添加文本,您可以做一件事,在水平方向的线性布局中,首先添加一个文本视图,然后在添加单选按钮旁边添加一个文本视图,无文本,如下所示:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" text="button text" >
</TextView>
<RadioButton
android:id="@+id/radio_sattelite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="onRadioButtonClicked"/>
</LinearLayout>
最后,在stackoverflow的帮助下,我让它开始工作:
radioButton.setGravity(Gravity.CENTER_VERTICAL);
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setButtonDrawable(R.drawable.empty);
radioButton.setCompoundDrawables(null, null, drawable, null);
那么什么是R.drawable.empty?这是一个没有背景的png文件,您必须创建它
谢谢……在stackoverflow的帮助下,我终于让它开始工作了:
radioButton.setGravity(Gravity.CENTER_VERTICAL);
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setButtonDrawable(R.drawable.empty);
radioButton.setCompoundDrawables(null, null, drawable, null);
那么什么是R.drawable.empty?这是一个没有背景的png文件,您必须创建它
谢谢….无需在该按钮可拖动中使用android.R.color.transparent创建空图像。无需在该按钮可拖动中使用android.R.color.transparent创建空图像。