【问题标题】:Android: TextView alignment with respect to RadioGroupAndroid:TextView 相对于 RadioGroup 的对齐方式
【发布时间】:2016-02-11 14:11:18
【问题描述】:

您好我正在尝试实现以下设计(RadioGroup 与右侧的 TextView 平行)

为了这个要求,我写了下面的 xml 代码

<RelativeLayout
            android:id="@+id/rl_group_create_group_privacy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rl_group_create_group_icon"
            android:layout_marginTop="10dp"
            android:background="@drawable/rl_bg_board_group_create_group"
            android:padding="10dp" >

            <TextView
                android:id="@+id/tv_group_create_group_privacy"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:text="@string/group_create_new_group_privacy"
                android:textColor="@color/Black"
                android:textSize="15sp" />

            <RadioGroup
                android:id="@+id/rg_group_create_group_privacy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/tv_group_create_group_privacy"
                android:orientation="vertical" >

                <RadioButton
                    android:id="@+id/rb_group_create_group_public"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="@string/group_create_new_group_public"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/rb_group_create_group_closed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="@string/group_create_new_group_closed"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/rb_group_create_group_secret"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="19dp"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="@string/group_create_new_group_secret"
                    android:textSize="12sp" />
            </RadioGroup>

            <RelativeLayout
                android:id="@+id/rl_group_create_group_privacy_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/rg_group_create_group_privacy"
                android:background="@color/White" >

                <TextView
                    android:id="@+id/tv_group_create_group_public_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="@string/group_create_new_group_public_desc"
                    android:textColor="@color/Black"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/tv_group_create_group_closed_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_group_create_group_public_desc"
                    android:layout_marginTop="10dp"
                    android:text="@string/group_create_new_group_closed_desc"
                    android:textColor="@color/Black"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/tv_group_create_group_secret_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_group_create_group_closed_desc"
                    android:layout_marginTop="10dp"
                    android:text="@string/group_create_new_group_secret_desc"
                    android:textColor="@color/Black"
                    android:textSize="12sp" />
            </RelativeLayout>
        </RelativeLayout>

但它会根据设备更改对齐方式,如下图所示(有时单选按钮位于 textView 的中间,有时位于 textView 的开头,有时位于 textView 的末尾)。通过在layout-large, layout-small, layout-xlarge 中实现设计,我们可以通过某种方式实现这一点,但我不想遵循。那么,如何在不关注layout-large, layout-small, layout-xlarge的情况下实现第一个图像设计

【问题讨论】:

    标签: android textview radio-group


    【解决方案1】:

    希望对你有帮助

    <LinearLayout
            android:id="@+id/rl_group_create_group_privacy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rl_group_create_group_icon"
            android:layout_marginTop="10dp"
            android:orientation="vertical"
            android:padding="10dp">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top">
    
                <TextView
                    android:id="@+id/tv_group_create_group_privacy"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Privacy"
                    android:textColor="#000000"
                    android:textSize="15sp" />
    
                <RadioButton
                    android:id="@+id/rb_group_create_group_public"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/tv_group_create_group_privacy"
                    android:checked="false"
                    android:gravity="center|start"
                    android:text="Public"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tv_group_create_group_public_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/rb_group_create_group_public"
                    android:padding="5dp"
                    android:text="group_create_new_group_public_desc"
                    android:textColor="#000000"
                    android:textSize="12sp" />
    
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top">
    
                <RadioButton
                    android:id="@+id/rb_group_create_group_closed"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60dp"
                    android:checked="false"
                    android:text="Closed"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tv_group_create_group_closed_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/rb_group_create_group_closed"
                    android:padding="5dp"
                    android:text="group_create_new_group_closed_desc"
                    android:textColor="#000000"
                    android:textSize="12sp" />
    
            </RelativeLayout>
    
    
            <RelativeLayout
                android:id="@+id/rl_group_create_group_privacy_desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top">
    
                <RadioButton
                    android:id="@+id/rb_group_create_group_secret"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60dp"
                    android:checked="false"
                    android:text="Secret"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tv_group_create_group_secret_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/rb_group_create_group_secret"
                    android:padding="5dp"
                    android:text="group_create_new_group_secret_desc"
                    android:textColor="#000000"
                    android:textSize="12sp" />
            </RelativeLayout>
    
        </LinearLayout>
    

    活动中

     final RadioButton rbPublic = (RadioButton) findViewById(R.id.rb_group_create_group_public);
            final RadioButton rbCloses = (RadioButton) findViewById(R.id.rb_group_create_group_closed);
            final RadioButton rbSecret = (RadioButton) findViewById(R.id.rb_group_create_group_secret);
    
            rbPublic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        rbCloses.setChecked(!isChecked);
                        rbSecret.setChecked(!isChecked);
                    }
                }
            });
            rbCloses.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        rbPublic.setChecked(!isChecked);
                        rbSecret.setChecked(!isChecked);
                    }
                }
            });
            rbSecret.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        rbCloses.setChecked(!isChecked);
                        rbPublic.setChecked(!isChecked);
                    }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      相关资源
      最近更新 更多