繁体   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