簡體   English   中英

Android:布局在不同設備上看起來有所不同

[英]Android: Layout looks different on different devices

嗨,我有一個活動,其中有一個imageview和一個底部帶有按鈕的行。按鈕以編程方式添加到iconView布局中。當我在HTC上測試我的應用程序時,按鈕顯示正確。

一旦我在屏幕較小的設備上運行它,就會對其進行裁剪,即僅顯示按鈕的頂部。

如何使我的代碼設備獨立?

<?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="match_parent"
android:background="#000000"
android:orientation="horizontal" >
<LinearLayout
    android:id="@+id/mainView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background2"
    android:orientation="vertical" >
    <ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.95"
    android:orientation="vertical"
    android:paddingLeft="4dp"
    android:paddingTop="4dp"
    android:paddingRight="4dp"
    android:paddingBottom="4dp"
    android:src="@android:drawable/ic_menu_camera" />

    <LinearLayout
    android:id="@+id/iconView"
    android:layout_weight="0.05"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal" >
    </LinearLayout>

    </LinearLayout>

    </LinearLayout>

提前致謝。

嘗試以下布局:

<?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="match_parent"
    android:background="#000000" >

    <RelativeLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background2" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="4dp"
                android:layout_above="@+id/iconView"
                android:src="@android:drawable/ic_menu_camera" />

            <LinearLayout
                android:id="@+id/iconView"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal" />

     </RelativeLayout>

</LinearLayout>

嘗試使用android:layout_weight =“ 1”創建第一個視圖
且其靜態高度不包含layout_weight,則應解決該問題

不幸的是,這些解決方案沒有用。 最終,我更改了代碼,並以編程方式添加了按鈕到xml中。 這很好。 我不知道為什么

以編程方式或以XML布局添加按鈕時,請嘗試將LinearLayout用作按鈕的父項,並利用該LinearLayout的“ weightsum”屬性。

當您要在多個視圖(例如按鈕或textview或...)之間平均共享屏幕寬度或高度的真實狀態時,可以在這些控件上設置“ layout_weight”屬性。 注意,這僅在使用LinearLayouts時才可行。

最后發生的是,線性布局的實際面積是根據每個孩子元素的“ layout_weight”值進行計算和分配的。

下面的示例演示如何將兩個按鈕彼此並排放置,使它們均勻地占據屏幕的寬度:

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:weightSum="2"
 >

     <Button
       android:id="@+id/btn_left"
       android:layout_width="0dp" // width is calculated by weight 
       android:layout_height= "wrap_content"
       android:layout_weight="1" />

     <Button
       android:id="@+id/btn_right"
       android:layout_width="0dp" // width is calculated by weight 
       android:layout_height= "wrap_content"
       android:layout_weight="1"  />

<LinearLayout/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM