1

私は最初のアンドロイドアプリをコーディングしようとしています。たくさん読んでいますが、レイアウトに問題があります。両方のxmlで何が問題になっていますか? 理解を深めるために、次の図を作成しました。写真

main.xml の私の 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>

フォルダー layout-land の main.xml の場合、landscape-Mode に切り替えると、アプリがクラッシュします。

 <?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>
4

1 に答える 1

0

さて、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 は、ボタンの上で宣言する必要があります

何が起こっているかというと、a) 水平で、b) ボタンが最初に配置され、c) ボタンに が表示されます。これは、ボタンが既に画面上のすべてのスペースを占有しているため、配置する場所がないことandroid:layout_width="match_parent"を意味します (ImageViewなぜこれが当てはまるのか分かりますか?)

これらの変更を加えてから、必要に応じて質問を更新してください。問題が変わると思います

于 2013-03-25T11:03:57.020 に答える