如何对齐单选按钮在屏幕的中心 [英] How to align Radio button at the center of the screen

查看:116
本文介绍了如何对齐单选按钮在屏幕的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用单选按钮选项卡在我的应用程序。

我已经加载图像,但他们正在调整向左侧。如何让他们在中锋位置对齐。

这是我的XML文件看起来

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@机器人:ID / tabhost
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>

  <的LinearLayout机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的FrameLayout机器人:ID =@机器人:ID / tabcontent
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_weight =1
      机器人:填充=20dip
      机器人:后台=#FFF/>

    < RadioGroup中的android:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:方向=横向
      机器人:checkedButton =@ + ID / allcontacts
      机器人:ID =@ + ID / contactgroup>


      <单选机器人:ID =@ + ID / allcontacts
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_gravity =中心
      机器人:layout_weight =1/>

      <单选按钮
          机器人:ID =@ + ID /类别
          机器人:layout_width =WRAP_CONTENT
          机器人:layout_height =WRAP_CONTENT
          机器人:layout_gravity =中心
          机器人:layout_weight =1/>

      <单选机器人:ID =@ + ID /收藏夹
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_gravity =中心
      机器人:layout_weight =1/>

    < / RadioGroup中>

    < TabWidget机器人:ID =@机器人:ID /标签
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_weight =0安卓知名度=水涨船高/>
  < / LinearLayout中>
< / TabHost>
 

解决方案

尝试 Android的:重力=中心的XML中的每个单选按钮

我看到你WRAP_CONTENT,所以理论上是不应该做任何事情,但它看起来像图标平分秋色,也许RadioGroup中划分空间均匀所以采用了android:重力会产生作用

值得一试我猜。

修改

试试这个:

 <单选机器人:ID =@ + ID / allcontacts
  机器人:layout_width =0dp
  机器人:layout_height =WRAP_CONTENT
  机器人:layout_gravity =中心
  机器人:marginLeft =20dp
  机器人:重力=中心
  机器人:layout_weight =1/>
 

您将与 Android的发挥:marginLeft =20dp来得到它正确居中,但会让你做你要找做什么。我复制你的布局和测试这个时候,所以我的知道的它的作品! :)

I am using Radio buttons as tab in my application.

I have loaded images for it, but they are aligning towards left side. how to make them align at center.

This is how my XML file looks

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="1" 
      android:padding="20dip" 
      android:background="#fff"/>

    <RadioGroup android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:orientation="horizontal"
      android:checkedButton="@+id/allcontacts"
      android:id="@+id/contactgroup">


      <RadioButton android:id="@+id/allcontacts" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1"/>

      <RadioButton
          android:id="@+id/categories"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_weight="1"/>

      <RadioButton android:id="@+id/favourites" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1" />

    </RadioGroup>

    <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="0" android:visibility="gone" />
  </LinearLayout>
</TabHost>

解决方案

Try android:gravity="center" in the xml for each RadioButton.

I see you're wrap_content, so theoretically that shouldn't do anything, but it looks like the icons are evenly divided, maybe the RadioGroup divides the space evenly so using android:gravity will have an effect.

Worth a shot I guess.

EDIT

Try this:

  <RadioButton android:id="@+id/allcontacts" 
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:marginLeft="20dp" 
  android:gravity="center"
  android:layout_weight="1"/>

You will have to play with the android:marginLeft="20dp" to get it properly centered, but that'll let you do what you're looking to do. I copied your layout and tested this time, so I know it works! :)

这篇关于如何对齐单选按钮在屏幕的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆