Android1.6模拟器上RadioGroup的RadioButton和文字的距离很近

来源:互联网 发布:手机单机游戏推荐知乎 编辑:程序博客网 时间:2024/05/01 06:46

今天修改一个BUG,出现了一个很纠结的问题。在使用Android的RadioGroup的RadioButton时,出现如下界面:

单选按钮和文字重叠在一起了,但在2.2上显示正常。。(全部是在大屏幕上进行的操作)

 

这个确实是我自己2了。

在XML中只需要设置一下:

android:paddingLeft="37dip"

这样就行了,如果是Java代码的话则设置:

radioBtnFemale.setPadding(55, 0, 0, 0);

开始由于xml和java文件中的单位不一样,所以文字也不一样,我开始设置的是一样子的,所以以为没解决,后来找了好久才找出来。太2了,汗。。。

效果如下:

顺便说下如果要控制两个RadioButton之间的距离,则设置如下:

android:layout_marginLeft="30px"

效果如下图所示:


赋上源码吧:

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:id="@+id/ll"    >    <RadioGroup         android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal"        >        <RadioButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="30px"            android:paddingLeft="37dip"            android:text="@string/nan"            />        <RadioButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="30px"            android:paddingLeft="37dip"            android:text="@string/nv"            />    </RadioGroup></LinearLayout>

Activity中就默认的就行了。

public class TestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);      }}


Java代码实现:

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:id="@+id/ll"    ></LinearLayout>

TestActivity.java:

package com.covics.zfh;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.widget.FrameLayout.LayoutParams;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;public class TestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);                RadioButton radioBtnMale, radioBtnFemale;                LinearLayout.LayoutParams sexParam = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);sexParam.gravity = Gravity.CENTER_VERTICAL;        RadioGroup radioGroup = new RadioGroup(this);radioGroup.setLayoutParams(sexParam);radioGroup.setOrientation(RadioGroup.HORIZONTAL);radioBtnMale = new RadioButton(this);// 界面布局宽度,高度LinearLayout.LayoutParams lpWarp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);radioBtnMale.setLayoutParams(lpWarp);radioBtnMale.setText("男");radioBtnMale.setPadding(55, 0, 0, 0);radioBtnMale.setTextColor(Color.parseColor("#999d9c"));radioGroup.addView(radioBtnMale);radioBtnFemale = new RadioButton(this);LinearLayout.LayoutParams femaleParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);radioBtnFemale.setLayoutParams(femaleParams);radioBtnFemale.setText("女");radioBtnFemale.setPadding(55, 0, 0, 0);radioBtnFemale.setTextColor(Color.parseColor("#999d9c"));radioGroup.addView(radioBtnFemale);        ll.addView(radioGroup);    }}


 

当然这只是测试小程序,里面有许多不规范的地方,希望大家不要和我学啊,比如在代码中使用了中文字符等。。。

希望这个对大家有帮助,谢谢。

原创粉丝点击