2016-11-15 76 views
0

我想讓圖像按鈕完全固定在屏幕和微調器上。它似乎有一個重量總和和重量佈局的問題。我試圖解決它,但沒有得到任何地方。這是我的代碼:如何停止android GUI中的組件不重疊

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".ElderActivity" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/first" 
     android:background="#ffab7c74" 
     android:layout_weight="1"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Smart House" 
      android:id="@+id/textView" 
      android:gravity="center" 
      android:autoText="true" 
      android:textColor="#fffff7f4" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/environment" 
      android:layout_weight="1" 
      android:id="@+id/imageButton" /> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/law" 
      android:layout_weight="1" /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner" 
      android:layout_weight="1" /> 
     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner1" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/consumer" 
      android:layout_weight="1"/> 
     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/educations" 
      android:layout_weight="1"/> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner2" 
      android:layout_weight="1" /> 
     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner3" 
      android:layout_weight="1" /> 

    </LinearLayout> 


</LinearLayout> 

[屏幕screem射擊] [1]:https://i.stack.imgur.com/tPFDi.jpg

回答

0

這是非常不好的做法,使用重量選擇,特別是如果你有佈局的深層次結構重量的選擇。對於您的示例,您需要使用RelativeLayout或甚至TableLayout

但是,如果你想搞亂你的底部,您可以使用棘手的事情,如:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 

    <View android:id="@+id/strut" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/text1" 
     android:layout_alignEnd="@id/strut" 
     android:layout_alignParentStart="true" 
     /> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/strut" 
     android:layout_alignParentEnd="true" 
     android:text="@string/text2" 
     /> 

</RelativeLayout>