Android is taking wrong layout on Inflating View

自古美人都是妖i 提交于 2019-12-11 19:35:24

问题


I have layouts inside 3 folders:

layout

layout-large

layout-xlarge

When I start root activity everything is fine and android is inflating view from layout-xlarge folder (I' m using Samsung tablet that belongs to xlarge device) When I start next activity android is for some reason takign xml file from layout folder. I already tried obvious things like checking dimensions inside xml files, setting support small, normal, large and xlarge screens inside my Manifest file.

There is a interesting thing that if I before starting activity write this code android 'll take xml file from x-large folder, but this looks silly (since android have his own logic of getting xml files from different layout fodlers)

Configuration config = getResources().getConfiguration();
            DisplayMetrics mDispMetrics = .getResources().getDisplayMetrics();
            getResources().updateConfiguration(config, mDispMetrics);

What could be a problem?

I'm using Android 3.0 api-11

here is normal layout sample_normal.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llAssetMainWrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gvSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:stretchMode="columnWidth" >
    </GridView>

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/background"
        android:cacheColorHint="#00000000"
        android:visibility="invisible" >
    </ListView>

</LinearLayout>

here is corresponding row_sample_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flRowVOD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="125dp"
        android:paddingBottom="10dp"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:paddingTop="10dp"
        android:scaleType="fitXY" />

    <View
        android:id="@+id/iconEmpty"
        android:layout_width="80dp"
        android:layout_height="150dp"
        android:paddingBottom="10dp"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:paddingTop="10dp" 
        android:visibility="gone"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:ellipsize="marquee"
        android:textColor="#FFFFFF"
        android:textSize="10sp"
        android:textStyle="bold"
        android:paddingBottom="10dp"
        android:paddingLeft="3dp"
        android:paddingRight="3dp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#434343"
        android:layout_gravity="bottom">
    </View>

</FrameLayout>

Here is xlarge: sample_xlarge.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llAssetMainWrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <GridView
        android:id="@+id/gvSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" >
    </GridView>

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/background"
        android:cacheColorHint="#00000000"
        android:visibility="invisible" >
    </ListView>

</LinearLayout>

here is corresponding row_sample_xlarge.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flRowVOD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="160dp"
        android:layout_height="250dp"
        android:paddingBottom="20dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="20dp"
        android:scaleType="fitXY" />

    <View
        android:id="@+id/iconEmpty"
        android:layout_width="160dp"
        android:layout_height="250dp"
        android:paddingBottom="20dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="20dp" 
        android:visibility="gone"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:ellipsize="marquee"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold"
        android:paddingBottom="20dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#434343"
        android:layout_gravity="bottom">
    </View>

</FrameLayout>

回答1:


Just in case someone stumbles on same problem:

I had issue using class UrlImageViewHelper that was probably downloaded from github (nos sure anymore). Class is used to download and hash images. There was code:

    if (mMetrics != null)
        return;
    mMetrics = new DisplayMetrics();
    Activity act = (Activity) context;
    act.getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
    AssetManager mgr = context.getAssets();
    mResources = new Resources(mgr, mMetrics, null);

By this you'll corrupt your resources (obviously)




回答2:


If your large corresponds to ~7 inch tablets, xlarge corresponds to ~10 inch tablets, it is more appropriate to use the qualifiers -sw600dp and -sw720dp respectively.



来源:https://stackoverflow.com/questions/18984397/android-is-taking-wrong-layout-on-inflating-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!