繁体   English   中英

适应屏幕尺寸的Android按钮布局

[英]Android button layout adapting to screen size

我是Android开发的新手。我用一个简单的按钮创建了一个简单的项目。我认为该按钮在不同的屏幕尺寸下看起来都是一样的,但是当我预览所有屏幕时,eclipse会显示此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>

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

在按钮部分更改:

android:layout_width="20dp"
android:layout_height="20dp"

使用特定的DP尺寸,使项目在不同的屏幕上具有相同的物理尺寸(将20替换为所需的尺寸)。

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

<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

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

如果您希望按钮在所有不同的屏幕中都具有相同的大小,而不必手动指定DP的大小。

喜欢

andoird:layout_width = "20dp"
android:layout_height = "20dp"

您还可以通过将相对布局更改为线性布局来将按钮放置在特定位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM