2016-04-12 2 views
1

Вот сравнение того, что мой редактор и устройство показывает:Android макет даже интервал

enter image description here

Проблема заключается в том:

  • строка, содержащая TextView должны принять 50% экрана
  • строка, содержащая значок, должна содержать оставшиеся 50% экрана

Он корректно работает в редакторе, но, как вы видите, макет ведет себя по-разному на самом устройстве.

Где моя проблема и как ее решить?

Библиотека используется для иконы: https://github.com/code-mc/material-icon-lib

Он простирается непосредственно от ImageView.

Layout 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

Вы пытались установить андроид: layout_weight = "0.5" в первом TableRow? –

+0

Я думаю, проблема связана с вашим столом. вы установили вес в 1, но layout_height - wrap_content. используйте match_parent вместо этого ... – Opiatefuchs

+0

Как отметил Мигель Бенитес, вы пробовали установить как android_layout_weigth на 0,5? – Ricardo

ответ

1

Добавьте свой TableRow в LinearLayout следующим образом. Я работал. я сделал тест.

<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

вы установили heights из двух TableRows в wrap_content, но они должны быть match_parent так:

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

иначе weight для height игнорируется.

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, никогда не знал, что что-то вроде этого существует;) –

Смежные вопросы