【问题标题】:Align horizontally 5 text views in Android在Android中水平对齐5个文本视图
【发布时间】:2017-08-30 17:41:47
【问题描述】:

我需要你的帮助来解决这个问题: 我有一个水平方向的线性布局。在这个布局中,我有 5 个可见性的文本视图:消失了。在这个布局上方,我有一个带有 5 个单选按钮的单选组,水平对齐。

当我选中一个单选按钮时,相应的文本视图会改变他的可见性。我想在每个单选按钮下方显示一个文本视图。其实他们都在同一个位置。

<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 >

   <TextView />
   <TextView />
   <TextView />
   <TextView />
   <TextView />
 </LinearLayout>

我该怎么做?

【问题讨论】:

  • 显示它现在的样子,以及您希望它的样子(显示一些图片)

标签: android textview radio-button android-linearlayout


【解决方案1】:

将 LinearLayout 的 weightSum 设置为 1.0。使每个文本视图的权重为 0.2,可见性为不可见。 可见性消失的视图不占用任何空间

我使第二个文本视图可见。但是带有文本“1”的textview会在带有文本“2”的textview之前占据空间。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.techdesire.HomeActivity"
    tools:showIn="@layout/app_bar_home">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radio_layout">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/value_group">

        <RadioButton
            android:text="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton1"
            android:layout_weight="0.2"/>
        <RadioButton
            android:text="2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton2"
            android:layout_weight="0.2"/>
        <RadioButton
            android:text="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton3"
            android:layout_weight="0.2"/>
        <RadioButton
            android:text="4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton4"
            android:layout_weight="0.2"/>
        <RadioButton
            android:text="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton5"
            android:layout_weight="0.2"/>
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container"
        android:orientation="horizontal"
        android:layout_below="@+id/radio_layout"
        android:weightSum="1.0">

        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:visibility="invisible"
            android:text="1"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:visibility="visible"
            android:text="22345678dsada"
            android:maxLines="1"
            android:ellipsize="marquee"/>
        <TextView
            android:id="@+id/text3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:visibility="invisible"
            android:text="3"/>
        <TextView
            android:id="@+id/text4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:visibility="invisible"
            android:text="4"/>
        <TextView
            android:id="@+id/text5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:visibility="invisible"
            android:text="5"/>
    </LinearLayout>
</RelativeLayout>

在单选组上设置 onCheckChangeListener 然后比较选中单选按钮的 id 并根据它隐藏/显示 textview。参见以下

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            /*hide all*/
            TextView tv;
            if(checkedID==R.id.radioButton1)
                tv=(RadioButton)findViewById(R.id.text1);
            elseif(checkedID==R.id.radioButton2)
                tv=(RadioButton)findViewById(R.id.text2);
            /* Similarly other else conditions*/
            tv.setVisibility(View.VISIBLE);
        }
    });

【讨论】:

    【解决方案2】:

    使用@ 987654321而不是gone,因为在gone他们不使用任何屏幕,所以当出现所选的时,其余的不是真的在那里,它将出现在第一个位置。 p >

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:
      RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
      radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      {
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
              // checkedId is the RadioButton selected
             // set visibility in visible according to Id by using if condition Or  switch
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2017-09-22
        • 2023-03-31
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 2015-12-17
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多