简体   繁体   English

Android:布局在不同设备上看起来有所不同

[英]Android: Layout looks different on different devices

Hi I have an activity with an imageview and a row with buttons at the bottom.The buttons are added to the iconView layout programmatically.When I test my app on my HTC one the buttons are displayed correct. 嗨,我有一个活动,其中有一个imageview和一个底部带有按钮的行。按钮以编程方式添加到iconView布局中。当我在HTC上测试我的应用程序时,按钮显示正确。

Once I run it on a device with a smaller screen it is cropped ie only the top of the buttons is displayed. 一旦我在屏幕较小的设备上运行它,就会对其进行裁剪,即仅显示按钮的顶部。

How can I make my code device independent? 如何使我的代码设备独立?

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

Thanks in advance. 提前致谢。

Try this layout: 尝试以下布局:

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

Try this make the first view with android:layout_weight="1" 尝试使用android:layout_weight =“ 1”创建第一个视图
and the with a static height without the layout_weight, this should solve it 且其静态高度不包含layout_weight,则应解决该问题

Unfortunately the solutions did not work. 不幸的是,这些解决方案没有用。 Eventually I changed my code and add the buttons in the xml, not programmatically. 最终,我更改了代码,并以编程方式添加了按钮到xml中。 This works fine. 这很好。 I don't know why... 我不知道为什么

When adding button either programmatically or in XML layout, try to use LinearLayout as the parent for button and leverage the "weightsum" property of that LinearLayout. 以编程方式或以XML布局添加按钮时,请尝试将LinearLayout用作按钮的父项,并利用该LinearLayout的“ weightsum”属性。

When you're going to share the real state of screen's width or height equally between a number of views such as buttons or textview or ..., you can set "layout_weight" property on those controls. 当您要在多个视图(例如按钮或textview或...)之间平均共享屏幕宽度或高度的真实状态时,可以在这些控件上设置“ layout_weight”属性。 Notice that, this is only possible when using LinearLayouts. 注意,这仅在使用LinearLayouts时才可行。

What happens at the end is that the linearlayout's real estate is calculated and allocated according to each child's element's "layout_weight" value. 最后发生的是,线性布局的实际面积是根据每个孩子元素的“ layout_weight”值进行计算和分配的。

The example below shows how to put two buttons beside each other and make them fill the screen's width equally : 下面的示例演示如何将两个按钮彼此并排放置,使它们均匀地占据屏幕的宽度:

<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