2017-04-23 81 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

我已经修复了错别字,但仍然出现此错误:/ –

相关问题