【问题标题】:As android device screen is bigger, buttons are upper由于android设备屏幕更大,按钮在上方
【发布时间】:2013-09-02 19:15:47
【问题描述】:
我放了 2 个按钮,大小为 wrap_content,但由于设备更大,按钮在上方,我不知道如何修复它们,所以在所有设备上都具有相同的位置。有没有办法不遮住这家伙的脑袋,比如。
布局.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:background="@drawable/background"
tools:context=".BasicScreenActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="102dp"
android:background="@drawable/custom_button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:background="@drawable/custom_button2" />
</RelativeLayout>
【问题讨论】:
标签:
android
android-layout
button
position
screen
【解决方案1】:
//下面这样试试
#remove the margin top 102dp
#don't use button background image use color with white stroke
#make your button width as match parent and minheight= 100dp as ur need.
#add android:gravity="center" to your parent layout
<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:background="@drawable/background"
android:gravity="center"
tools:context=".BasicScreenActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/holo_green_dark" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:background="@android:color/holo_green_dark" />
</RelativeLayout>
【解决方案2】:
Android 运行在具有不同屏幕分辨率的多台设备上,以下是如何支持所有设备:Supporting Multiple Screens
基本上我们有以下屏幕分辨率:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
为了支持所有这些,在 Res 文件夹中,我们必须根据我们想要支持的屏幕创建文件夹。
layout-xlarge
layout-large
layout-normal
layout-small
之后,将最终的 layout_file.xml 复制到所有这些文件中,以图形模式打开它,然后重新排列按钮以在屏幕上看起来不错。根据屏幕分辨率,android 会选择更接近设备分辨率的布局。只需在不同的设备或虚拟设备上进行测试,以确保它看起来不错。
【解决方案3】:
如果你想让应用启动时所有视图覆盖设备的整个屏幕,你需要LinearLayout的weight
<LinearLayout 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:background="@drawable/background"
android:orientation="vertical"
tools:context=".BasicScreenActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="0 dip" //this is important!
android:layout_weight="5" //<-- add this
android:layout_marginTop="102dp"
android:background="@drawable/custom_button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0 dip" //this is important!
android:layout_weight="5" //<-- add this
android:background="@drawable/custom_button2" />
</LinearLayout>
我的总权重通常等于 10,因此更容易计算和可视化 UI