是否可以在仍然使用Android的内置组件的同时在RadioButton和标签之间添加一点空间?默认情况下,文本看起来有点紧张.
<RadioButton android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Text"/>
Run Code Online (Sandbox Code Playgroud)
我尝试了几件事:
指定边距和填充似乎在整个元素周围添加空间(按钮和文本,一起).这是有道理的,但不能做我需要的.
通过XML创建自定义drawable,为已检查和未检查的状态指定图像,然后在每个图像的右侧添加一些额外的像素.这应该可行,但现在您已经超出默认UI.(不是世界末日,但不是理想的)
在每个标签的开头添加额外的空格.Android似乎修剪了一个领先的空格字符,如"我的字符串",但是指定unicode U + 00A0,就像"\ u00A0My String"一样.这有效,但似乎有点脏.
更好的解决方案?
我的应用程序中有一个主题,它定义了默认的textappearance和Button Style.
<style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:textAppearance">@style/TextAppearance.Medium</item>
<item name="android:textAppearanceLarge">@style/TextAppearance.Large</item>
<item name="android:textAppearanceMedium">@style/TextAppearance.Small</item>
<item name="android:textAppearanceSmall">@style/TextAppearance.Medium</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:textColorTertiary">#000000</item>
<item name="android:buttonStyle">@style/Button</item>
</style>
<style name="TextAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textColorHint">?android:attr/textColorHint</item>
</style>
<style name="TextAppearance.Large">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textSize">32dp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="TextAppearance.Medium">
<item name="android:textColor">?android:attr/textColorSecondary</item>
<item name="android:textSize">22dp</item>
</style>
<style name="TextAppearance.Small">
<item name="android:textColor">?android:attr/textColorTertiary</item>
<item name="android:textSize">18dp</item>
</style>
<style name="Button" parent="@android:style/Widget.Button">
<item name="android:textColor">?android:attr/textColorSecondary</item>
<item name="android:textColorHint">?android:attr/textColorHint</item>
<item name="android:textStyle">normal</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
Run Code Online (Sandbox Code Playgroud)
当我使用创建RadioButton时
RadioButton radioButton = new RadioButton(this, null, R.style.AppTheme);
Run Code Online (Sandbox Code Playgroud)
创建RadioButton并正确设置样式,但选中的标记消失.为什么?
我在Buttongroup中有Radiobutton,我在那里动态添加按钮.当我通过布局添加按钮时,标记会正确显示.但动态添加按钮却没有.
我想在一个片段中创建一些dinamically的单选按钮,我只有样式问题.如果我将radiobutton代码放在xml文件中,则会正确应用默认样式,但是当我通过函数创建radiobutton时,我会看到不同的样式!
XML
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:animationCache="false">
<RadioButton
android:text="RadioButton 1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton3" />
<RadioButton
android:text="RadioButton 2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton4" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
结果
JAVA CODE
此代码放在片段中的onCreateView中
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
for (int i = 1; i <= num; i++) {
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio " + radioButton.getId());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioGroup.addView(radioButton);
}
}
Run Code Online (Sandbox Code Playgroud)
结果
正如你所看到的单选按钮有不同的风格,有人可以帮助我,如果可能的话,以编程方式应用默认样式?