【问题标题】:Android button layout adapting to screen size适应屏幕大小的Android按钮布局
【发布时间】:2015-01-18 11:26:02
【问题描述】:

我是 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>

能否请您帮帮我,告诉我如何设计该按钮在所有屏幕尺寸上看起来都一样? 谢谢

【问题讨论】:

标签: java android eclipse button


【解决方案1】:

当按钮的父级是 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)。

【讨论】:

【解决方案2】:

关于按钮部分的变化:

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

使用特定的 DP 尺寸使项目在不同屏幕上保持相同的物理尺寸(将 20 替换为您想要的尺寸)。

【讨论】:

    【解决方案3】:

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

    喜欢

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多