适应屏幕尺寸的Android按钮布局 [英] Android button layout adapting to screen size

查看:60
本文介绍了适应屏幕尺寸的Android按钮布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手.我用一个简单的按钮创建了一个简单的项目.按钮将在不同的屏幕尺寸上看起来相同,但是当我预览所有屏幕时,Eclipse显示此内容 http://imgur.com/kjEMhHx

I am new in Android development.I created a simple project with a simple button.I thought that the button will look the same on different screen sizes but when i previewed all screens eclipse displayed this http://imgur.com/kjEMhHx

这是xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.businessideas.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="116dp"
    android:text="Button" />

</RelativeLayout>

您能帮我一下,告诉我如何设计在所有屏幕尺寸上都相同的按钮吗?谢谢

Can you please help me and tell me how can i design that button look the same on all screen sizes? Thanks

推荐答案

当按钮的父对象为LinearLayout时,您将需要使用weight属性.这样,它将迫使按钮的大小与屏幕的宽度成比例:

You'll need to use the weight attribute when the button's parent is LinearLayout. That way it will force the button to have a size proportional to the screens width:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="10"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:text="Button"
        android:layout_weight="2"
        android:layout_width="0dp"/>
</LinearLayout>

请注意,如果 LinearLayout 的方向为水平,宽度将由android进行调整(隐式),因此子级的宽度为 0dp .

Notice that if the LinearLayout's orientation is horizontal, width will be adjusted by android (implicitly) therefore the children have width of 0dp.

还要注意,现在按钮将占用LinearLayout(父级)宽度的20%(2/10).

Also note that now the button will take 20% (2/10) of the LinearLayout's (the parent's) width.

这篇关于适应屏幕尺寸的Android按钮布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆