2012-03-20 35 views
3

我正在寻找一种在XML帮助下在特定位置放置按钮的方法。Android - 如何使用XML在不同屏幕密度上的特定位置放置按钮

我希望问题的按钮被放置在屏幕中间,只有退出。

这个我希望即使在改变屏幕密度时也能工作。

我尝试过使用android:layout_marginLeft =“10dp”,但它并没有出现在相同的位置,当我降低密度从240到120.它说在android应用程序开发的页面,应该使用dp,屏幕,在这种情况下似乎不是。

得到了按钮下面的代码大气压:

<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> 
+0

尝试使用dip代替dp – 2012-03-20 16:50:13

+0

@AlexanderKulyakhtin ** dip **只是** dp **的别名。他们是一样的。 – kokabi 2017-07-15 11:01:12

回答

2

退房下面的代码在屏幕的中间正好放置按钮:

<?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> 

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

<?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"您方便的价值。希望这可以帮助。 Akhil

+0

当我这样做时,它似乎不起作用,只是没有区别。这两个图像的大小,我也试图切换到“浸”。 我也可以说,当我改变模拟器“抽象LCD密度” – sandmaster 2012-03-24 18:11:22

+0

其改变位置和大小,你在这里指的是哪些图像?你的布局只有一个按钮和一个textView – Akhil 2012-03-24 19:17:48

+0

按钮图像 – sandmaster 2012-03-25 11:36:49

相关问题