简体   繁体   English

Android布局对齐问题

[英]Android Layout Alignment Issue

I am trying to find the correct xml to define a layout of three items on a single 'line' as follows : 我试图找到正确的xml,以在单个“行”上定义三个项目的布局,如下所示:

  • Button (left aligned on the screen) with the text 'Prev' - wrap-context 带有文本“上一页”的按钮(在屏幕上左对齐)
  • TextView (center aligned) with the title of the screen - fixed 200px size TextView(居中对齐)与屏幕标题-固定为200px
  • Button (right aligned on the screen) with the text 'Next' - wrap_content 带有文本“下一步”的按钮(在屏幕上右对齐)-wrap_content

Just cannot seem to get it working unless I use absolute layout which I'd rather not. 除非我使用绝对不希望的绝对布局,否则似乎无法正常工作。 I have experimented (seems like every option) with gravity/layout_gravity and embedded LinearLayouts... 我已经尝试过使用gravity / layout_gravity和嵌入式LinearLayouts(似乎像每个选项一样)...

I guess the question is - can I right align one item, left align another and center align another all on the same LinearLayout 'line' 我想问题是-我可以在同一LinearLayout'line'上右对齐一个项目,左对齐另一个并居中对齐另一个项目吗

Thanks .. KJ 谢谢.. KJ

In that case you better use RelativeLayout as follows: 在这种情况下,最好如下使用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="wrap_content">
    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Left" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_left"
        android:layout_alignTop="@id/btn_left"
        android:text="In the center" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Right" />
</RelativeLayout>

By the way... it's not recommended to give exact dimensions to your Views. 顺便说一句...不建议为您的视图提供确切的尺寸。 But rather, use relative things like fill_parent , wrap_layout and the like. 而是使用诸如fill_parentwrap_layout之类的相对内容。

Well, I could just comment Cristian answer, but I don't have enough point, so, here is my version of his layout proposal: 好吧,我只可以评论克里斯蒂安的答案,但我的观点还不够,所以,这是他的版式建议版本:

<?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="wrap_content" >

    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Left" />

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

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="In the center" />

</RelativeLayout>

Just removed the align to buttons stuff and put a android:layout_centerHorizontal="true". 刚刚删除了align to button的东西,并放置了android:layout_centerHorizo​​ntal =“ true”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM