android gridlayout未显示在模拟器API 22中 [英] android gridlayout not showing in emulator API 22
问题描述
我有一个应用程序,该应用程序在运行API 23的实际设备Nexus 6P和仿真的6P,5& 4运行API 24,但在模拟的Nexus 5&上显示奇怪的行为wrt GridLayout
4个正在运行的API22。
I have an app that functions exactly as desired on an actual device Nexus 6P running API 23 and an emulated 6P, 5 & 4 running API 24, but is showing odd behaviour wrt GridLayout
on emulated Nexus 5 & 4 running API 22.
该应用只有一个活动,其中有多个 RelativeLayout
s通过编程方式设置为 .VISIBLE
& 。看不见
。在一个这样的 RelativeLayout
中,我有2个 GridLayout
,如下所示。模拟API 22时,底部 GridLayout
不会显示,但顶部API始终显示,而与API 22、23或24无关。底部 GridLayout
仅在将模拟器设置更改为API 24时才显示(没有尝试过23)。为什么会这样呢?我认为我没有在 GridLayout
上使用任何需要API 23或更高版本的东西。这个问题意味着我无法在运行API 22或21的Nexus 5上安装此应用。
The app only has a single activity with multiple RelativeLayout
s that are programmatically set .VISIBLE
& .INVISIBLE
. In one such RelativeLayout
I have 2 GridLayout
s as follows. When emulating API 22, the bottom GridLayout
does not show but the top one always does irrespective of API 22, 23 or 24. The bottom GridLayout
only shows when I change the emulator setting to API 24 (haven't tried 23). Why might that be? I don't think I'm using anything on GridLayout
that requires API 23 or above. This problem means I cannot install this app on a Nexus 5 running API 22 or 21. Thanks for any suggestion.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gameOperations"
android:visibility="invisible">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/launchDisplay"
android:gravity="center_horizontal">
<TextView
android:text="@string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/launchTitle"
android:textStyle="normal|bold"
android:textSize="50dp"
android:textAlignment="center"
android:fontFamily="casual"
android:textColor="@android:color/holo_blue_dark"
android:layout_marginTop="15dp"
android:layout_weight="1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/brain_orig_crop_500"
android:id="@+id/launchImage"
android:scaleType="centerInside"
android:layout_weight="2" />
<TextView
android:text="@string/launchSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/launchSubtitle"
android:fontFamily="casual"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"
android:textSize="25dp"
android:textStyle="normal|bold"
android:layout_weight="1"
android:layout_marginTop="15dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/launchControls"
android:gravity="center_vertical"
android:layout_weight="1">
<Button
android:text="@string/debug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/launchSpacerLeft"
android:layout_weight="1"
android:background="@color/colourTransparent"
android:textSize="15dp"
android:fontFamily="casual"
android:textAllCaps="false"
android:visibility="invisible" />
<Button
android:text="@string/launchStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/launchStart"
android:textColor="@android:color/holo_green_dark"
android:textSize="38dp"
android:textStyle="normal|bold"
android:onClick="prepareNewGame"
android:layout_weight="3"
android:background="@color/colourTransparent"
android:fontFamily="casual"
android:textAllCaps="false" />
<Button
android:text="@string/layoutNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/launchSpacerRight"
android:layout_weight="1"
android:textAllCaps="false"
android:background="@color/colourTransparent"
android:fontFamily="casual"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:id="@+id/header"
android:columnCount="6"
android:rowCount="4"
android:layout_height="168dp"
android:useDefaultMargins="true"
android:layout_alignParentTop="true">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="@drawable/brain_orig_500"
android:id="@+id/imageBrain"
android:layout_column="0"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_rowSpan="4"
android:layout_columnSpan="6"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:scaleType="fitCenter" />
<Button
android:text="@string/debug"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/debug"
android:layout_column="2"
android:layout_row="3"
android:layout_columnSpan="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:fontFamily="casual"
android:textSize="18dp"
android:textStyle="normal|bold"
android:textAllCaps="false"
android:onClick="prepareNewGame"
android:background="@color/colourTransparent"
android:layout_gravity="fill"
android:paddingTop="10dp"
android:visibility="gone" />
<TextView
android:text="@string/blank"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/spacerColumn"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="3"
android:layout_rowWeight="3" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/timerDisplay"
android:id="@+id/timerDisplay"
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:fontFamily="casual"
android:textSize="30dp"
android:textStyle="normal|bold"
android:gravity="start"
android:layout_gravity="fill"
android:layout_rowSpan="1" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/rightWrongDisplay"
android:layout_column="2"
android:layout_row="3"
android:layout_columnSpan="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textSize="28dp"
android:fontFamily="casual"
android:layout_gravity="fill"
android:layout_rowSpan="1"
android:visibility="gone" />
<TextView
android:text="@string/scoreDisplay"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/scoreDisplay"
android:layout_column="4"
android:layout_row="3"
android:layout_columnSpan="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:fontFamily="casual"
android:textStyle="normal|bold"
android:textSize="30dp"
android:gravity="end"
android:layout_gravity="fill"
android:layout_rowSpan="1" />
</GridLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/scoreBoard"
android:layout_alignParentTop="false"
android:layout_below="@+id/header"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/question"
android:layout_below="@+id/scoreBoard"
android:layout_marginTop="15dp"
android:gravity="center">
<TextView
android:text="@string/operandLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/operandLeft"
android:layout_weight="5"
android:textSize="60sp"
android:fontFamily="casual"
android:textAlignment="textEnd"
android:textStyle="normal|bold"
android:paddingEnd="10dp"
android:paddingStart="0dp"
android:layout_gravity="center_vertical|end"
android:paddingLeft="0dp"
android:paddingRight="10dp"
android:visibility="gone" />
<TextView
android:text="@string/operator"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/operator"
android:layout_weight="1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textSize="60sp"
android:fontFamily="casual"
android:visibility="gone" />
<TextView
android:text="@string/operandRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/operandRight"
android:layout_weight="5"
android:textAlignment="textStart"
android:textStyle="normal|bold"
android:textSize="60sp"
android:fontFamily="casual"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:layout_gravity="center_vertical|start"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:visibility="gone" />
<TextView
android:text="@string/sampleQuestion"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/questionView"
android:layout_weight="1"
android:gravity="center"
android:textSize="60dp"
android:textStyle="normal|bold"
android:fontFamily="casual" />
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/question"
android:layout_marginTop="10dp"
android:id="@+id/answers"
android:rowCount="2"
android:columnCount="2"
android:useDefaultMargins="true"
android:layout_above="@+id/footer">
<Button
android:text="@string/answer0"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/answer0"
android:layout_column="0"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="actionAnswer"
android:textSize="50dp"
android:textAllCaps="false"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:layout_gravity="fill_horizontal"
android:background="@color/colourTransparent" />
<Button
android:text="@string/answer1"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/answer1"
android:layout_column="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="actionAnswer"
android:textSize="50dp"
android:textAllCaps="false"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:layout_gravity="fill_horizontal"
android:background="@color/colourTransparent" />
<Button
android:text="@string/answer2"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/answer2"
android:layout_column="0"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:onClick="actionAnswer"
android:textSize="50dp"
android:textAllCaps="false"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:background="@color/colourTransparent" />
<Button
android:text="@string/answer3"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/answer3"
android:layout_column="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:onClick="actionAnswer"
android:textSize="50dp"
android:textAllCaps="false"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:background="@color/colourTransparent" />
</GridLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="@+id/footer"
android:layout_marginTop="15dp"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true">
<TextView
android:text="@string/footerText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/footerText"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
android:fontFamily="casual" />
</LinearLayout>
</RelativeLayout>
推荐答案
我也遇到了这个问题,我终于找到了通过使用 android.support.v7.widget.GridLayout
解决方案,而不仅仅是 GridLayout
I too had exactly this issue and I finally found a solution by using android.support.v7.widget.GridLayout
instead of just GridLayout
我不太了解细节,但是以前版本的GridLayout似乎不支持 android:layout_columnWeight
属性,尽管它确实支持 android:layout_width
属性。假设您使用 layout_width(确实存在)将宽度设置为 0dp,然后尝试使用 columnWeight(不存在)拉伸宽度,则小部件将保持在0dp,因此不会显示。
I don't understand the details deeply but previous versions of GridLayout don't seem to support the android:layout_columnWeight
property, though it does support the android:layout_width
property. Given you are setting the width to "0dp" using "layout_width" (which does exist) then attempting to 'stretch' it using "columnWeight" (which does not exist) the widget remains at 0dp and thus is not shown.
要解决此问题,GridLayout的 support.v7版本允许您使用 layout_columnWeight,尽管您必须确保将 app作为与 android,例如 app:layout_columnWeight = 1
。因此,您的布局文件应显示为:
To work around this the "support.v7" version of GridLayout does allow you to use 'layout_columnWeight', though you must be sure to prefix it with 'app' as apposed to 'android', as in app:layout_columnWeight = "1"
. So your layout file should read:
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:id="@+id/header"
android:columnCount="6"
android:rowCount="4"
android:layout_height="168dp"
android:useDefaultMargins="true"
android:layout_alignParentTop="true">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="@drawable/brain_orig_500"
android:id="@+id/imageBrain"
android:layout_column="0"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_rowSpan="4"
android:layout_columnSpan="6"
app:layout_columnWeight="1"
android:layout_gravity="fill"
android:scaleType="fitCenter" />
.
.
.
</android.support.v7.widget.GridLayout>
关键更改是添加了 android.support.v7.widget。
更改为 Gridlayout
,并将 android:layout_columnWeight = 1
更改为 app :layout_columnWeight = 1
The critical changes being adding android.support.v7.widget.
to Gridlayout
and changing android:layout_columnWeight="1"
to app:layout_columnWeight="1"
您还必须将要使用的gridlauout-v7版本添加到依赖项中表现。对我来说我补充了:
You will also have to add the version of gridlauout-v7 you wish to use to the dependency section of your manifest. For me I added:
compile'com.android.support:gridlayout-v7:26.0.0-alpha1'
最后,如果要在样式中包括列权重,请省略 app前缀,例如:
And finally if you are wanting to include the column weight in a style then omit the 'app' prefix, as in:
<style name="button_digit" parent="button">
<item name="android:layout_width">0dip</item>
<item name="layout_columnWeight">1</item>
.
.
.
</style>
这篇关于android gridlayout未显示在模拟器API 22中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!