问题水平/垂直布局Android应用程序

问题描述:

我试图编写我的第一个android应用程序。我读了很多,但是我的布局有问题。 这两个xml有什么问题? 为了更好地理解,我做了一个画面:pic问题水平/垂直布局Android应用程序

我的XML为main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:orientation="vertical" > 



<ImageView 

    android:id="@+id/imageView1" 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:layout_weight="2.5" 

    android:contentDescription="@string/upload" 

    android:scaleType="fitCenter" 

    android:visibility="visible" /> 



<LinearLayout 

    android:layout_width="match_parent" 

    android:layout_height="0dp" 

    android:layout_weight="4" 

    android:gravity="center" 

    android:orientation="vertical" > 



    <Button 

     android:id="@+id/btnShoot" 

     android:layout_width="match_parent" 

     android:layout_height="wrap_content" 

     android:layout_gravity="center" 

     android:layout_marginLeft="50dp" 

     android:layout_marginRight="50dp" 

     android:text="@string/shoot_again" /> 

</LinearLayout> 
</LinearLayout> 

在文件夹布局土地main.xml中,如果我切换到横向模式的应用程序崩溃:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:orientation="horizontal" > 



<Button 

    android:id="@+id/btnShoot" 

    android:layout_width="match_parent" 

    android:layout_height="wrap_content" 

    android:layout_gravity="center" 

    android:layout_marginLeft="50dp" 

    android:layout_marginRight="50dp" 

    android:text="@string/shoot" /> 



<ImageView 

    android:id="@+id/imageView1" 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:contentDescription="@string/upload" 

    android:scaleType="fitCenter" 

    android:visibility="visible" /> 

</LinearLayout> 
+0

,怎么了? – 2013-03-25 10:55:56

+0

显示您的错误日志。 – 2013-03-25 10:56:40

好了,只是看你的XML,有几个问题:

肖像XML:

< ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="2.5" 

在这里,你已经同时设置了重量和高度 - 在LinearLayout你不能兼得。要么你自己设置高度,要么给它一个高度0dp和一个重量,然后让LinearLayout指定高度。至于它为什么旋转,图像本身可能会在您的资源中旋转。

水平XML:

  • 你的根LinearLayout仍然需要有一个vertical方向
  • 你的ImageView需要您的按钮

发生了什么事是,)它的水平以上声明, b)您的按钮首先被放置,c)您的按钮有android:layout_width="match_parent",这意味着ImageView无处可放,因为您的Button已经占用了所有在屏幕上的空间(你可以看到这是为什么呢?)

进行这些更改,然后更新您的问题在适当时,我认为,当你说“崩溃”你的问题就已经改变了