如何在屏幕的某些位置对齐视图,在多个屏幕分辨率下保持一致?

如何在屏幕的某些位置对齐视图,在多个屏幕分辨率下保持一致?

问题描述:

我对我的按钮有点问题。我想在(大约)1/3和我的屏幕的2/3,让他们(我下面提供的截图,以使事情更清晰) 我的代码如下:如何在屏幕的某些位置对齐视图,在多个屏幕分辨率下保持一致?

<?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" 
    android:background="@drawable/mainbg"> 

    <Button 
     android:text="@string/topbutton" 
     android:background="@drawable/silvertablesbuttonbgkort" 
     android:id="@+id/topbutton" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center|center_vertical"> 
    </Button> 

    <Button 
     android:text="@string/midbutton" 
     android:background="@drawable/silvertablesbuttonbglang" 
     android:id="@+id/midbutton" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center|center_vertical"> 
    </Button> 


<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

我试图让它看起来像这样:(我很抱歉,发布屏幕截图的模型在这里是皱眉,我只是想不出一个更好的方式来清理我想要做的)

看来,我不能发布图片,因为我太新了在这里...所以这里是一个链接截图:http://i.imgur.com/l3b3z.png

我最初的想法是在两个按钮周围环绕一个垂直线性布局,并在两者之间放置空的文本视图,如果此应用旨在在一个屏幕大小上运行,实际上它将工作,但我很漂亮确定它会在另一个屏幕分辨率的每个手机上搞砸。我通过使用android:layout_alignParentBottom =“true”的相关布局解决了底部酒吧的这个问题,但我真的无法想到一种方法来捕捉1/3或2/3。有没有办法做到这一点,将与各种屏幕分辨率工作?

编辑: 我解决了我的问题!我试图将其作为答案张贴,但我不能在8小时之内。这样做,但现在,我只是将它作为编辑发布在这里:

我已经在屏幕中间放置了一个0x0的TextView,并将RelativeLayouts放在它的上面和下面,填充屏幕。然后,我在这些布局的中间放置了两个TextView,它们的布局中有两个新的RelativeLayouts。一个低于最高TextView,一个高于最低。我把我的按钮放在这些布局的中心。它像魅力一样工作,除了代码本身之外不依赖任何东西。这是我现在的代码:

<?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" 
    android:background="@drawable/mainbg"> 


    <TextView 
     android:text=" " 
     android:id="@+id/ankermidden" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

    <RelativeLayout 
     android:id="@+id/ankerveldboven" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/ankermidden"> 

       <TextView 
        android:text=" " 
        android:id="@+id/ankerboven" 
        android:layout_centerVertical="true" 
        android:layout_width="0dp" 
        android:layout_height="0dp"> 
       </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidboven" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/ankerboven"> 

       <Button 
        android:text="@string/topbutton" 
        android:background="@drawable/silvertablesbuttonbgkort" 
        android:id="@+id/topbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout> 

</RelativeLayout> 


<RelativeLayout 
     android:id="@+id/ankerveldonder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/ankermidden"> 

    <TextView 
     android:text=" " 
     android:id="@+id/ankeronder" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidonder" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@id/ankeronder">  

       <Button 
        android:text="@string/midbutton" 
        android:background="@drawable/silvertablesbuttonbglang" 
        android:id="@+id/midbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout>  

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

所以是的,这是比我想象的更容易。

我已经找到了解决方案!一个完全相对于自身,并且不依赖像素或密度像素。

我已经在屏幕中间放置了一个0x0的TextView,并将RelativeLayouts放在它的顶部和底部,填满了屏幕。

然后,我在这些布局的中间放置了两个TextView,它们是布局中的两个新的RelativeLayouts。一个低于最高TextView,一个高于最低。我把我的按钮放在这些布局的中心。

它像一个魅力一样工作,并不依赖于任何东西,而不是代码本身。

这是我现在的代码:

<?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" 
    android:background="@drawable/mainbg"> 


    <TextView 
     android:text=" " 
     android:id="@+id/ankermidden" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

    <RelativeLayout 
     android:id="@+id/ankerveldboven" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/ankermidden"> 

       <TextView 
        android:text=" " 
        android:id="@+id/ankerboven" 
        android:layout_centerVertical="true" 
        android:layout_width="0dp" 
        android:layout_height="0dp"> 
       </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidboven" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/ankerboven"> 

       <Button 
        android:text="@string/topbutton" 
        android:background="@drawable/silvertablesbuttonbgkort" 
        android:id="@+id/topbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout> 

</RelativeLayout> 


<RelativeLayout 
     android:id="@+id/ankerveldonder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/ankermidden"> 

    <TextView 
     android:text=" " 
     android:id="@+id/ankeronder" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidonder" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@id/ankeronder">  

       <Button 
        android:text="@string/midbutton" 
        android:background="@drawable/silvertablesbuttonbglang" 
        android:id="@+id/midbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout>  

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

所以是的,这是比我想象的更容易。

Bill Gary建议在dp中使用一个边距,以保持不同屏幕尺寸上的相同比例,但是经过大量实验后,事情在不同的屏幕上再次显得很怪异。

在我回到这个之前,我会做一些更多的尝试,因为这个整体的倾斜边缘的东西正在让我失望......应该正确显示的东西不是,而且不应该的东西,在一些屏幕分辨率上做。

我会开始做这些东西的作业,但现在虽然有点长,但我上面发布的代码对我来说完美无瑕。

试试这个,你可能需要调整的DP机器人:layout_marginTop = “150dp”

<Button 
    android:text="@string/topbutton" 
    android:background="@drawable/silvertablesbuttonbgkort" 
    android:id="@+id/topbutton" 
    android:layout_alignParentRight="true" 
    android:layout_marginTop="150dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center|center_vertical"> 
</Button> 
+0

这是一种可能性,但仍然会在小型和更具异国情调的屏幕上出现问题(例如:银河系Pro)会不会?因为那是我第一次写的东西,现在整个事情都是相对的,但我不能在8小时内将它作为答案发布,因为我是新来的。如果您有兴趣,我会发布我的文章作为对我的文章的修改。 – Louis

+0

嗯,似乎无法编辑第二次发布最后一篇文章...我现在觉得有点愚蠢,我对dps的工作方式有一个完全错误的想法。开始怀疑自己并使用Google搜索,结果发现这是一件聪明的事情。我认为dps允许人们指定与屏幕实际大小相关的事物。是的,我知道,笑声和面部表情都允许在这里,但至少你已经了解了我现在编程的新内容。非常感谢您的回答。 – Louis

+0

别担心,我是新人,仍然在学习 –