Android-如何使用XML在不同屏幕密度的特定位置上放置按钮 [英] Android - How to place a button on a specific position on different screen densities with XML

查看:63
本文介绍了Android-如何使用XML在不同屏幕密度的特定位置上放置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在XML的帮助下将按钮放置在特定位置的方法.

I am looking for a way to place a button on a specific spot with the help of XML.

我希望将有问题的按钮放置在屏幕中间,仅向下一点.

I want the button in question to be placed in the middle of the screen, only abit down.

即使更改屏幕密度,我也想这样做.

This I would like to be working even when changing screen densities.

我尝试使用android:layout_marginLeft ="10dp",但是当我将密度从240降低到120时,它并没有显示在同一位置.它说在androids开发页面上应该使用dp,并且会随着屏幕,在这种情况下似乎不是.

I have tried using android:layout_marginLeft="10dp", but it doesnt show up at the same position when i lower the densities from 240 till 120. It says on androids dev page that dp should be used and will scale with the screen, which in this case doesnt seem to be.

为按钮获取以下代码atm:

Got the following code atm for the button:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/yowie_meny_utan_knappar">


<Button
    android:id="@+id/Button04"
    android:layout_width="100dp"
    android:layout_height="73dp"
    android:layout_gravity="center"
    android:layout_marginTop="200dp"
    android:layout_marginLeft="50dp"
    android:background="@drawable/play_button"
    >
  </Button>

推荐答案

查看下面的代码,该按钮将按钮准确地放置在屏幕中间:

Check out the below code which places the button exactly at the middle of the screen :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Button 
    android:layout_centerInParent="true" 
    android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</RelativeLayout>

如果我仍然希望Button从中间向下一点,我知道的一种好方法是使用一个空的文本视图作为按钮的参考点.

If I still want the Button to be a little bit down from the middle, the one good way I know is to use an empty text view as a reference point for the button.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <TextView  android:layout_centerInParent="true" android:layout_width="wrap_content" android:id="@+id/textView1" 
     android:layout_height="wrap_content" android:text=" " ></TextView>
    <Button 
     android:layout_marginTop="5dip"
     android:layout_centerHorizontal="true"
    android:text="Button" android:layout_below="@id/textView1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</RelativeLayout>

为方便起见,将android:layout_marginTop="5dip"的值更改.

Change the value of android:layout_marginTop="5dip" to your convenience.

这篇关于Android-如何使用XML在不同屏幕密度的特定位置上放置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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