2017-04-05 39 views
0

我需要您的幫助來解決此問題: 我有一個水平方向的線性佈局。在這個佈局中,我有5個可視性文本瀏覽器:消失了。在這個佈局之上,我有一個收音機組,5個單選按鈕水平對齊。在Android中水平對齊5個文本視圖

當我檢查一個單選按鈕時,相應的文字視圖會改變其可見性。我想在每個單選按鈕下方顯示文本視圖。其實他們是在相同的位置。

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

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

我該怎麼做?

+2

顯示它現在的樣子,你想怎麼看(顯示一些圖像) –

回答

0

將LinearLayout weigthSum設置爲1.0。使每個文本視圖的權重爲0.2 &可見性不可見。 查看不見可用空間

我使第二個textview可見。但是文本「1」的textview將在textview之前佔據空間,文本爲「2」。

<?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.See以下

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); 
     } 
    }); 
0

的使用visibility:Invisible代替gone,因爲他們gone不使用任何屏幕,其餘的是不是真的有選擇的一個出現時,它會出現在第一的位置。

希望這會有所幫助。

0
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 
    } 
});