2016-04-12 95 views
1

這裏是我的編輯的比較和設備顯示:Android的佈局均勻間距

enter image description here

的問題是:含有的TextView的

  • 行應採取50%的屏幕
  • 包含圖標的行應占剩餘屏幕的50%

它在編輯器中正常工作,但正如您所見,佈局在實際設備上的行爲有很大不同。

我的問題在哪裏以及如何解決?用於圖標

庫是: https://github.com/code-mc/material-icon-lib

它直接從ImageView的延伸。

佈局XML:

<TableLayout 
    android:id="@+id/menuLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="*" > 

    <TableRow 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
     <LinearLayout 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"> 
      <android.support.v7.widget.AppCompatTextView 
       android:id="@+id/teenSelection" 
       android:textColor="@color/WHITE" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:text="TEEN" /> 
      <android.support.v7.widget.AppCompatTextView 
       android:id="@+id/adultSelection" 
       android:textColor="@color/WHITE" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:text="ADULT"/> 
      <android.support.v7.widget.AppCompatTextView 
       android:id="@+id/seniorSelection" 
       android:textColor="@color/WHITE" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:text="SENIOR"/> 
     </LinearLayout> 
    </TableRow> 

    <TableRow 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
     <net.steamcrafted.materialiconlib.MaterialIconView 
      android:layout_weight="1" 
      android:id="@+id/confirmButton" 
      app:materialIcon="check" 
      app:materialIconColor="#fff" 
      android:scaleType="center" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"/> 
    </TableRow> 

</TableLayout> 
+0

您是否嘗試在第一個TableRow中設置android:layout_weight =「0.5」? –

+0

我認爲問題出在你的tableRows上。您已將權重設置爲1,但layout_height爲wrap_content。使用match_parent來代替... – Opiatefuchs

+0

正如Miguel Benitez所指出的那樣,您是否嘗試將android_layout_weigth設置爲0.5? – Ricardo

回答

1

像這樣在LinearLayout中添加您的TableRow。我工作了 。我做了測試。

<TableLayout 
    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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:stretchColumns="*"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

     <TableRow 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"> 
      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent"> 
       <android.support.v7.widget.AppCompatTextView 
        android:id="@+id/teenSelection" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:layout_weight="1" 
        android:background="@color/black_slow" 
        android:textStyle="bold" 
        android:textSize="30sp" 
        android:text="TEEN" /> 
       <android.support.v7.widget.AppCompatTextView 
        android:id="@+id/adultSelection" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:layout_weight="1" 
        android:background="@color/black_slowest" 
        android:textStyle="bold" 
        android:textSize="30sp" 
        android:text="ADULT"/> 
       <android.support.v7.widget.AppCompatTextView 
        android:id="@+id/seniorSelection" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:background="@color/colorPrimaryDark" 
        android:layout_weight="1" 
        android:textStyle="bold" 
        android:textSize="30sp" 
        android:text="SENIOR"/> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"> 
      <net.steamcrafted.materialiconlib.MaterialIconView 
       android:id="@+id/confirmButton" 
       app:materialIcon="check" 
       app:materialIconColor="#fff" 
       android:scaleType="center" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent"/> 
     </TableRow> 
    </LinearLayout> 

</TableLayout> 

here is device and editor pic

+0

完美的作品,非常感謝你:) –

+0

歡迎你的好友。 –

2

你已經確立了自己兩個TableRowsheightswrap_content,但它們必須match_parent這樣的:

<TableRow 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

否則weightheight被忽略。

2

你應該嘗試PercentRelativeLayout

在應用級的build.gradle添加此

compile 'com.android.support:percent:23.3.0' 

,並與該改變你的佈局。

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:id="@+id/layoutTop" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="3" 
     app:layout_heightPercent="50%" 
     app:layout_widthPercent="100%"> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/teenSelection" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="TEEN" 
      android:textColor="@android:color/white" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/adultSelection" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="ADULT" 
      android:textColor="@android:color/white" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/seniorSelection" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="SENIOR" 
      android:textColor="@android:color/white" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <net.steamcrafted.materialiconlib.MaterialIconView 
     android:id="@+id/confirmButton" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_below="@+id/layoutTop" 
     android:scaleType="center" 
     app:layout_heightPercent="50%" 
     app:layout_widthPercent="100%" 
     app:materialIcon="check" 
     app:materialIconColor="#fff" /> 

</android.support.percent.PercentRelativeLayout> 
+0

謝謝,我已經用mehrdad方法解決了它。 但是,對於PercentRelativeLayout,感謝你,從來不知道這樣的東西存在;) –