First: What am I trying to do? A: I'm trying to do a basic layout reshuffle when the orientation changes without using any .java code of my own.
第一:我想做什么?答:我正在尝试在方向更改时进行基本布局重新洗牌,而不使用我自己的任何.java代码。
Here's my activity_main.xml:
这是我的activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:orientation="@string/wrapper_orientation"
tools:context=".MainActivity">
<ImageView
android:layout_width="@string/image_width"
android:layout_height="@string/image_height"
android:layout_weight="@string/image_weight"
android:background="@drawable/customborder"
/>
<LinearLayout
android:layout_width="@string/content_width"
android:layout_height="@string/content_height"
android:layout_weight="@string/content_weight"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@drawable/customborder"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@drawable/customborder">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have a strings.xml file in both /values-land and /values-port, here's port for an example:
我在/ values-land和/ values-port中都有一个strings.xml文件,这里的端口是一个例子:
<resources>
<string name="wrapper_orientation">vertical</string>
<string name="image_width">fill_parent</string>
<string name="image_height">0dp</string>
<string name="image_weight">.45</string>
<string name="content_width">fill_parent</string>
<string name="content_height">0dp</string>
<string name="content_weight">.55</string>
</resources>
This works fine in the emulator, but when I send this to my phone, the app force closes on inflation with the following errors:
这在模拟器中工作正常,但当我将其发送到我的手机时,应用程序强制关闭通知,并出现以下错误:
android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
Caused by: java.lang.NumberFormatException: Invalid int: "vertical"
I assume this is because however its retrieving the resource, the wrong type is being returned. But I'm not sure why or how to get around it. Two hours of googling hasn't been much help.
我假设这是因为它检索资源,返回错误的类型。但我不确定为什么或如何绕过它。两个小时的谷歌搜索没有多大帮助。
Just another note: If I hard-code the orientation to vertical, it then fails saying that my first ImageView has no layout_width, so I'm pretty sure I'm at least on the right track.
只是另一个注意事项:如果我将方向硬编码为垂直方向,则说明我的第一个ImageView没有layout_width,所以我很确定我至少在正确的轨道上。
Am I being too much of a simpleton here in hoping to accomplish what I'm after this easily? Any help appreciated. I'm using the latest Android Studio, and my minSDK is 14. All my main .java file does at this point is the standard inflate, this proof of concept is all I've tried to do so far:
我是不是在这里过于愚蠢,希望能够轻松完成我之后的事情?任何帮助赞赏。我正在使用最新的Android Studio,我的minSDK是14.我的主要.java文件在这一点上确实是标准的膨胀,这个概念证明是我迄今为止所做的全部尝试:
setContentView(R.layout.activity_main);
Thanks in advance!
提前致谢!
Pic (linked, boo, no rep) of the emulator working fine:
Pic(链接,boo,无代表)模拟器工作正常:
http://i.imgur.com/1cCJsYR.png
1 个解决方案
#1
0
In xml, the vertical and horizontal orientation designations are not strings, but integer constants pre defined. You are taking an incorrect approach to this problem. The better option is to use multiple layout files defined in resource directories with appropriate qualifiers. So you will have a layout-port
and a layout-land
folder in res
with the layout xml file defined with the same name in each, with the the appropriate changes to the layout for each orientation. You can read more about resource qualifiers here:
在xml中,垂直和水平方向指定不是字符串,而是预定义的整数常量。您正在采取不正确的方法解决此问题。更好的选择是使用具有适当限定符的资源目录中定义的多个布局文件。因此,您将在res中使用layout-port和layout-land文件夹,并在每个文件中使用相同的名称定义布局xml文件,并对每个方向的布局进行适当的更改。您可以在此处阅读有关资源限定符的更多信息
支持多个屏幕
#1
0
In xml, the vertical and horizontal orientation designations are not strings, but integer constants pre defined. You are taking an incorrect approach to this problem. The better option is to use multiple layout files defined in resource directories with appropriate qualifiers. So you will have a layout-port
and a layout-land
folder in res
with the layout xml file defined with the same name in each, with the the appropriate changes to the layout for each orientation. You can read more about resource qualifiers here:
在xml中,垂直和水平方向指定不是字符串,而是预定义的整数常量。您正在采取不正确的方法解决此问题。更好的选择是使用具有适当限定符的资源目录中定义的多个布局文件。因此,您将在res中使用layout-port和layout-land文件夹,并在每个文件中使用相同的名称定义布局xml文件,并对每个方向的布局进行适当的更改。您可以在此处阅读有关资源限定符的更多信息
支持多个屏幕