2017-04-23 78 views
0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="org.innoversetech.hobuddies.MainActivity" 
    android:background="#FF7506" 
    android:id="@+id/linMatcher"> 
    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/lin1"> 

<!-- Psychometric Questions--> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="37dp" 
     android:id="@+id/Surgency1" 
     android:textSize="20sp" 
     android:text="I am the life of the party"/> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/Surgency1" 
      android:orientation="vertical"> 
      <!--Radio Buttons--> 
      <RadioButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Strongly Disagree" 
       android:id="@+id/SD"/> 
      <RadioButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Disagree" 
       android:id="@+id/D"/> 
      <RadioButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Not really" 
       android:id="@+id/NR"/> 
      <RadioButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Agree" 
       android:id="@+id/A"/> 
      <RadioButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Strongly Agree" 
       android:id="@+id/SA"/> 
     </LinearLayout> 
    </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

我想要的是用相似的單選按鈕創建15個問題。有沒有辦法讓我創建一個單選按鈕佈局,然後調用它?我不必一次又一次複製粘貼單選按鈕。另外我怎樣才能定義每個ID的不同?有沒有辦法在另一個佈局中調用已定義的佈局?

回答

0

您可以使用<include>標籤輕鬆做到這一點,將標籤添加到屬性layout

試試這個:

<include 
    layout="@layout/your_layout"> 

</include> 

例如:

這是你radio_layout.xml

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

     <!--Radio Buttons--> 
     <RadioButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Strongly Disagree" 
      android:id="@+id/SD"/> 
     <RadioButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Disagree" 
      android:id="@+id/D"/> 
     <RadioButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Not really" 
      android:id="@+id/NR"/> 
     <RadioButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Agree" 
      android:id="@+id/A"/> 
     <RadioButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Strongly Agree" 
      android:id="@+id/SA"/> 
    </LinearLayout> 

這裏是你的acivity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="org.innoversetech.hobuddies.MainActivity" 
    android:background="#FF7506" 
    android:id="@+id/linMatcher"> 
    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/lin1"> 

     <!-- Psychometric Questions--> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="37dp" 
      android:id="@+id/Surgency1" 
      android:textSize="20sp" 
      android:text="I am the life of the party"/> 

     <include 
      layout="@layout/radio_layout"> 
     </include> 

    </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

希望這將有助於〜

0

可以佈局與<include />標籤

重新使用。如果包括佈局應在目標佈局中多次使用:

  1. 創建佈局radio_btns.xml<merge />標籤作爲根

    <merge xmlns:android="http://schemas.android.com/apk/res/android"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Strongly Disagree"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Disagree"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Not really"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Agree"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Strongly Agree"/> </merge>

  2. 在主要佈局添加的ViewGroup物品如的LinearLayoutRelativeLayout的或任何的ViewGroup

    .... <LinearLayout android:id="@+id/first" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> <LinearLayout android:id="@+id/second" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> .....

  3. 在活動後的setContentView

    LayoutInflater inflater = LayoutInflater.from(this); ViewGroup first = (ViewGroup) findViewById(R.id.first); ViewGroup second = (ViewGroup) findViewById(R.id.second); inflater.inflate(R.layout.radio_btns.xml, first, true); inflater.inflate(R.layout.radio_btns.xml, second, true); //Get any item to from viewGroup as child RadioButton firstInFirstGroup = (RadioButton) first.getChildAt(0); RadioButton secondInSecondGroup = (RadioButton) second.getChildAt(1);

  4. 不要使用ID在XML佈局,如果你將它列入到XML多次目標。並注意的ViewGroup是第一項指標爲0

+0

我在這兩條線得到錯誤(無法解析符號「膨脹」,不能解析符號「第一」和「seconf」) inflater.inflate (R.layout.radio_q1.xml,first,true); inflater.inflate(R.layout。radio_q1.xml,second,true); –

+0

我修正了一個錯字。現在試試。 –

+0

我已經修復了錯別字,但仍然出現此錯誤:/ –

相關問題