xml 中声明元素顺序的重要性是什么
What is the importance about sequence of declaring elements in xml
我正在学习 Android 编程,所以我决定编写一个简单的应用程序来计算人们的 BMI!我遇到的问题是,当我先声明我的 TextView 和 Button,然后再声明 ImageView 时,所有三个元素在纵向和横向模式下都适合屏幕:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.bmicalculator.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator" />
<Button
android:id="@+id/button1"
android:layout_width="195dp"
android:layout_height="33dp"
android:text="Start"
android:textSize="15dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/splash" />
http://www.imageupload.co.uk/images/2015/01/30/4H6T2c.jpg
http://www.imageupload.co.uk/images/2015/01/30/3VQ0uM.jpg
但是当我更改顺序并在横向模式下 ImageView 输出更改后声明 TextView 和 Button 时,TextView 消失了!只有一半的按钮显示和图像覆盖顶部设置栏:
http://www.imageupload.co.uk/images/2015/01/30/2kP5Os.jpg
不过竖屏模式还是可以的
http://www.imageupload.co.uk/images/2015/01/30/1l90d.jpg
那么,当我更改在 xml 中声明这些元素的顺序时,会发生什么变化?我该如何解决这个问题。谢谢
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bmicalculator.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@drawable/splash"
android:layout_above="@+id/textView1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="195dp"
android:layout_height="33dp"
android:text="Start"
android:textSize="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我正在学习 Android 编程,所以我决定编写一个简单的应用程序来计算人们的 BMI!我遇到的问题是,当我先声明我的 TextView 和 Button,然后再声明 ImageView 时,所有三个元素在纵向和横向模式下都适合屏幕:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.bmicalculator.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator" />
<Button
android:id="@+id/button1"
android:layout_width="195dp"
android:layout_height="33dp"
android:text="Start"
android:textSize="15dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/splash" />
http://www.imageupload.co.uk/images/2015/01/30/4H6T2c.jpg http://www.imageupload.co.uk/images/2015/01/30/3VQ0uM.jpg
但是当我更改顺序并在横向模式下 ImageView 输出更改后声明 TextView 和 Button 时,TextView 消失了!只有一半的按钮显示和图像覆盖顶部设置栏:
http://www.imageupload.co.uk/images/2015/01/30/2kP5Os.jpg
不过竖屏模式还是可以的
http://www.imageupload.co.uk/images/2015/01/30/1l90d.jpg
那么,当我更改在 xml 中声明这些元素的顺序时,会发生什么变化?我该如何解决这个问题。谢谢
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bmicalculator.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@drawable/splash"
android:layout_above="@+id/textView1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="195dp"
android:layout_height="33dp"
android:text="Start"
android:textSize="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>