2016-07-18 2 views
1

Я использовал layout_weight в LinearLayout в моем XML-файле. Предварительный просмотр в порядке, но когда я запускаю приложение в телефоне, вид отличается. Это XML-файлВес линейного макета не работает на телефоне, отлично работает в xml-превью в студии android

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout 
 
    android:orientation="vertical" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
 

 
     <!--<TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="@string/title_dashboard" 
 
      android:textSize="25sp"/>--> 
 

 
    <android.support.v7.widget.CardView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="200dp"> 
 

 
     <RelativeLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:layout_margin="16dp"> 
 

 
     <ImageView 
 
      android:layout_width="120dp" 
 
      android:layout_height="120dp" 
 
      android:id="@+id/ivBrandImage" 
 
      android:scaleType="centerCrop" 
 
      android:src="@mipmap/ic_launcher" 
 
      android:layout_centerVertical="true"/> 
 

 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:textSize="30sp" 
 
      android:id="@+id/tBrandName" 
 
      android:layout_toRightOf="@+id/ivBrandImage" 
 
      android:text="Email name" 
 
      android:layout_marginLeft="@dimen/margin_left_brand_name" 
 
      android:layout_centerVertical="true"/> 
 

 
     <android.support.v7.widget.SwitchCompat 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/tBrandName" 
 
      android:layout_toRightOf="@+id/ivBrandImage" 
 
      android:layout_marginLeft="@dimen/margin_left_brand_name" 
 
      android:layout_marginTop="6dp" 
 
      android:id="@+id/switchBrand"/> 
 

 
    </RelativeLayout> 
 

 
    </android.support.v7.widget.CardView> 
 

 

 
    <android.support.v7.widget.CardView 
 
     android:layout_width="match_parent" 
 
     android:layout_marginTop="@dimen/margin_top_dashboard_card" 
 
     android:layout_height="0dp" 
 
     android:layout_weight="1" 
 
     android:layout_gravity="center"> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="match_parent" 
 
      android:gravity="center_vertical" 
 
      android:layout_marginLeft="@dimen/margin_left_dashboard" 
 
      android:text="@string/textview_pending_approvals" 
 
      /> 
 

 
    </android.support.v7.widget.CardView> 
 

 
    <android.support.v7.widget.CardView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="0dp" 
 
     android:layout_marginTop="@dimen/margin_top_dashboard_card" 
 
     android:layout_weight="1" 
 
     android:layout_gravity="center"> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="match_parent" 
 
      android:layout_marginLeft="@dimen/margin_left_dashboard" 
 
      android:text="@string/textview_pending_approvals" 
 
      android:gravity="center_vertical" 
 
      /> 
 

 
    </android.support.v7.widget.CardView> 
 

 
    <android.support.v7.widget.CardView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="0dp" 
 
     android:layout_marginTop="@dimen/margin_top_dashboard_card" 
 
     android:layout_weight="1" 
 
     android:layout_gravity="center"> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="match_parent" 
 
      android:layout_marginLeft="@dimen/margin_left_dashboard" 
 
      android:text="@string/textview_pending_approvals" 
 
      android:gravity="center_vertical" 
 
      /> 
 

 
    </android.support.v7.widget.CardView> 
 

 
</LinearLayout>

Ниже приводится XML-просмотр:

xml preview

А вот фактическое скриншот же по телефону:

screenshot

Я не могу понять, почему фактический экран отличается от предварительного просмотра xml. Может кто-нибудь, пожалуйста, скажите, в чем тут ошибка?

+0

Как вы надуваете этот xml-макет? –

+1

Это макет чего? Очевидно, что родительский вид вашего 'LinearLayout' имеет свойство android: layout_height =" wrap_content ". Итак, какова иерархия макетов? –

+0

Это один из фрагментов навигационного ящика. Я создал активность ящика навигации из встроенных опций студии. Итак, иерархия несколько похожа: DrawerLayout (activity_main.xml)> CoordinatorLayout (app_bar_main.xml)> CardView (код которого я вставил в вопрос). Все три части находятся в разных xml-файлах. Атрибут «include» используется для включения файлов в иерархию, о которой я упоминал. – backslashN

ответ

0

Изменение родительского макета XML-файла из LinearLayut для RelativeLayout решила проблему. Конечно, для достижения цели потребовались некоторые незначительные изменения. Вот xml-файл, который отлично работает и на телефоне.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_alignParentTop="true" 
     android:id="@+id/cvDashboardHeader"> 

     <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="16dp"> 

      <ImageView 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:id="@+id/ivBrandImage" 
       android:scaleType="centerCrop" 
       android:src="@mipmap/ic_launcher" 
       android:layout_centerVertical="true" 
       android:foreground="?android:attr/selectableItemBackground" 
       android:clickable="true"/> 


      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" 
       android:id="@+id/tBrandName" 
       android:layout_toRightOf="@+id/ivBrandImage" 
       android:text="Email name" 
       android:layout_marginLeft="@dimen/margin_left_brand_name" 
       android:layout_centerVertical="true"/> 

      <android.support.v7.widget.SwitchCompat 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tBrandName" 
       android:layout_toRightOf="@+id/ivBrandImage" 
       android:layout_marginLeft="@dimen/margin_left_brand_name" 
       android:layout_marginTop="6dp" 
       android:id="@+id/switchBrand"/> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_below="@+id/cvDashboardHeader"> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_marginTop="@dimen/margin_top_dashboard_card" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:foreground="?android:attr/selectableItemBackground" 
      android:clickable="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:gravity="center_vertical" 
       android:layout_marginLeft="@dimen/margin_left_dashboard" 
       android:text="@string/textview_pending_approvals" 
       /> 

     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginTop="@dimen/margin_top_dashboard_card" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:foreground="?android:attr/selectableItemBackground" 
      android:clickable="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="@dimen/margin_left_dashboard" 
       android:text="@string/textview_pending_approvals" 
       android:gravity="center_vertical" 
       /> 

     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginTop="@dimen/margin_top_dashboard_card" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:foreground="?android:attr/selectableItemBackground" 
      android:clickable="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="@dimen/margin_left_dashboard" 
       android:text="@string/textview_pending_approvals" 
       android:gravity="center_vertical" 
       /> 

     </android.support.v7.widget.CardView> 

    </LinearLayout> 

</RelativeLayout> 
0

, пожалуйста, сделайте это для разрешения, как hdpi, xhdip, xxhdpi, xxxhdpi. , пожалуйста, выберите тестовое разрешение устройства здесь, затем проверьте его снова.

enter image description here

+0

Макет отлично смотрится во всех разрешениях в студии. Но, не работает в устройстве. Я использую Moto E2 (Lollipop) – backslashN

0

Попробуйте следующее:

android:layout_height="0dp" Использование и android:layout_weight="1" для первого CardView (который содержит изображение Android логотипа).

+0

Это приведет к исчезновению всей карты. вес должен быть больше 0. – backslashN

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!--<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_dashboard" 
     android:textSize="25sp"/>--> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="200dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="16dp"> 

      <ImageView 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:id="@+id/ivBrandImage" 
       android:scaleType="centerCrop" 
       android:src="@mipmap/ic_launcher" 
       android:layout_centerVertical="true"/> 


      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" 
       android:id="@+id/tBrandName" 
       android:layout_toRightOf="@+id/ivBrandImage" 
       android:text="Email name" 
       android:layout_marginLeft="@dimen/button_padding" 
       android:layout_centerVertical="true"/> 

      <android.support.v7.widget.SwitchCompat 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tBrandName" 
       android:layout_toRightOf="@+id/ivBrandImage" 
       android:layout_marginLeft="@dimen/button_padding" 
       android:layout_marginTop="6dp" 
       android:id="@+id/switchBrand"/> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 


    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_marginTop="@dimen/button_padding" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical" 
      android:layout_marginLeft="@dimen/button_padding" 
      android:text="fjdkjk" 
      /> 

    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="@dimen/button_padding" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/button_padding" 
      android:text="jdfkjffjkjk" 
      android:gravity="center_vertical" 
      /> 

    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="@dimen/button_padding" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/button_padding" 
      android:text="jkgjk" 
      android:gravity="center_vertical" 
      /> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 

импорт это -

компиляции 'com.android.support:cardview-v7:21.0.+'

работает отлично в МИ4

+0

Я уже включил компиляцию 'com.android.support:cardview-v7:23.3.0' в файл gradle. – backslashN

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