gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:32:57 26 4
gpt4 key购买 nike

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

你能帮我告诉我如何设计在所有屏幕尺寸上看起来都一样的按钮吗?谢谢

最佳答案

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

关于java - 适应屏幕尺寸的Android按钮布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27042344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com