对于想了解为什么我的androidRadioButton文本一开始就被截断了?的读者,本文将提供新的信息,我们将详细介绍android中radiobutton控件,并且为您提供关于AndroidRad
对于想了解为什么我的android RadioButton文本一开始就被截断了?的读者,本文将提供新的信息,我们将详细介绍android中radiobutton控件,并且为您提供关于Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、Android RadioButton使用的有价值信息。
本文目录一览:- 为什么我的android RadioButton文本一开始就被截断了?(android中radiobutton控件)
- Android RadioButton
- Android RadioButton 控件
- Android RadioButton 背景图变形问题
- Android RadioButton使用
为什么我的android RadioButton文本一开始就被截断了?(android中radiobutton控件)
我遇到了一个非常奇怪的布局问题…基本上,按照此屏幕截图,此RadioGroup中的第一个按钮丢失了它的第一个字母(它应该显示为“ Booked Off”,而不是“ ooked Off”):
XML片段非常简单(我在外部样式或其他任何方面都没有什么好笑的):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+shift_edit/bookoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Booked Off" />
<RadioButton android:id="@+shift_edit/ado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADO" />
<RadioButton android:id="@+shift_edit/working"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Working" />
</RadioGroup>
</LinearLayout>
有没有人认识到这里的问题并可以提供解决方案?如果有问题,我将使用1.6,因为我要定位的设备只有1.6 ROM.
解决方法:
因此,将我的应用程序部署到真实的(Android 1.6)设备后,看来问题出在模拟器上.我无法在真实设备上复制它.同样,如user432209所述,它似乎没有出现在以后的模拟器中.更好的是,它只会在您第一次进行活动时显示.出门再回来,它又恢复了正常.因此,我想,总的来说,这是一个很小的缺陷,可以忽略.
Android RadioButton
1.RadioGroup继承于LinearLayout
<RadioGroup >
<RadioButton/>
...
</RadioGroup>
((RadioButton) radioGroup.getChildAt(index)).setChecked(true);
2.监听器
RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
RadioButton rb = radioGroup.findViewById(id);
int checkedIndex = radioGroup.indexOfChild(rb);
}
}
3.自定义自动换行RadioGroup
import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.RadioGroup; public class MyRadioGroup extends RadioGroup { private static final String TAG = "RadioGroupEx"; public MyRadioGroup(Context context) { super(context); } public MyRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); //调用ViewGroup的方法,测量子view measureChildren(widthMeasureSpec, heightMeasureSpec); //最大的宽 int maxWidth = 0; //累计的高 int totalHeight = 0; //当前这一行的累计行宽 int lineWidth = 0; //当前这行的最大行高 int maxLineHeight = 0; //用于记录换行前的行宽和行高 int oldHeight; int oldWidth; int count = getChildCount(); //假设 widthMode和heightMode都是AT_MOST for (int i = 0; i < count; i++) { View child = getChildAt(i); MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); //得到这一行的最高 oldHeight = maxLineHeight; //当前最大宽度 oldWidth = maxWidth; int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin; if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加 //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth maxWidth = Math.max(lineWidth, oldWidth); //重置宽度 lineWidth = deltaX; //累加高度 totalHeight += oldHeight; //重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin; Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth); } else { //不换行,累加宽度 lineWidth += deltaX; //不换行,计算行最高 int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin; maxLineHeight = Math.max(maxLineHeight, deltaY); } if (i == count - 1) { //前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值 totalHeight += maxLineHeight; //计算最后一行和前面的最宽的一行比较 maxWidth = Math.max(lineWidth, oldWidth); } } //加上当前容器的padding值 maxWidth += getPaddingLeft() + getPaddingRight(); totalHeight += getPaddingTop() + getPaddingBottom(); setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth, heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); //pre为前面所有的child的相加后的位置 int preLeft = getPaddingLeft(); int preTop = getPaddingTop(); //记录每一行的最高值 int maxHeight = 0; for (int i = 0; i < count; i++) { View child = getChildAt(i); MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。 if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) { //重置 preLeft = getPaddingLeft(); //要选择child的height最大的作为设置 preTop = preTop + maxHeight; maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin; } else { //不换行,计算最大高度 maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin); } //left坐标 int left = preLeft + params.leftMargin; //top坐标 int top = preTop + params.topMargin; int right = left + child.getMeasuredWidth(); int bottom = top + child.getMeasuredHeight(); //为子view布局 child.layout(left, top, right, bottom); //计算布局结束后,preLeft的值 preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin; } } }
Android RadioButton 控件
RadioButton 单选按钮
常用属性:
text 文本
checked=“true” 默认选中
一组互斥的单选按钮要放在 RadioGroup 中。RadioGroup 常用属性:
orientation 该组单选按钮的排列方向。
示例:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gender"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
Android RadioButton 背景图变形问题
问题描述:
一般我们在做收藏、关注功能的时候会使用到 RadioButton,但是我们在设置背景为图片的时候,会出现变形拉伸,或者挤压。
解决方法:
通过尝试发现:设置 RadioButton 的 text 属性,只需要有这个属性就可以,然后再添加 textsize 属性,将字体大小属性值设置为比较小,我设置为 2sp。运行后我们会发现图片变形问题不复存在。
分析:
没有看源代码,在这里意淫一下,意淫的就当玩乐吧!可能是个小 bug,RadioButton 的背景图会受到字体大小的影响,当字体大于背景图的大小就会出现变形的情况。
通过设置字体的大小小于背景图的大小,这样 RadioButton 的大小取决于较大的那个元素也就是背景图,这样背景图和 RadioButton 的大小保持一致就避免了背景图变形。
Android RadioButton使用
例题2-12
activity_main.xml代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <GridLayout android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:columnCount="4" 5 android:rowCount="6" 6 xmlns:android="http://schemas.android.com/apk/res/android"> 7 <RadioGroup> 8 <RadioButton android:id="@+id/boy" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:checked="true" 12 android:text="男"/> 13 <RadioButton android:id="@+id/girl" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="女"/> 17 </RadioGroup> 18 <Button android:id="@+id/but1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="确认" 22 android:layout_columnSpan="4"/> 23 <TextView android:id="@+id/text1" 24 android:layout_height="wrap_content" 25 android:layout_width="wrap_content" 26 android:text="" /> 27 </GridLayout>
mainactivity.java代码
1 package com.example.hello; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.telephony.SmsManager; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.CheckBox; 10 import android.widget.ProgressBar; 11 import android.widget.RadioButton; 12 import android.widget.TextView; 13 14 public class MainActivity extends AppCompatActivity { 15 Button but1; 16 TextView textView; 17 RadioButton radioButton1,radioButton2; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 textView = (TextView)findViewById(R.id.text1); 23 but1 = (Button)findViewById(R.id.but1); 24 radioButton1 = (RadioButton)findViewById(R.id.boy); 25 radioButton2 = (RadioButton)findViewById(R.id.girl); 26 but1.setonClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 String s=""; 30 if(radioButton1.isChecked()) 31 s=s+radioButton1.getText(); 32 if (radioButton2.isChecked()) 33 s=s+radioButton2.getText(); 34 textView.setText("性别:"+s); 35 } 36 }); 37 38 } 39 40 }
关于为什么我的android RadioButton文本一开始就被截断了?和android中radiobutton控件的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、Android RadioButton使用的相关知识,请在本站寻找。
本文标签: