简体   繁体   English

相对布局android中的视图对齐

[英]Alignment of views in relative layout android

I want to align 2 textviews-one on the top left and one on the bottom right. 我想对齐2个textviews-一个在左上方,一个在右下方。 These textviews are placed over the imageswitcher view. 这些文本视图放置在imageswitcher视图上。 I get what I want except that the bottom textview is aligned to the left. 除了底部的文本视图向左对齐之外,我得到了我想要的东西。 I am not able to align it to the right. 我无法将其向右对齐。

Here's the code: 这是代码:

    <?xml version="1.0" encoding="utf-8"?>  

    <RelativeLayout   

     xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@anim/fade_in"
       />

    <TextView  
        android:id="@+id/tv_1"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_width="140dp" 
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="TV 1"
        android:textStyle="normal"
        android:textSize="14sp"
        android:textAlignment="center"
        android:background="@drawable/button_bg"
        android:textColor="@color/white"/>

       <TextView  
        android:id="@+id/tv_2"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_width="140dp" 
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="TV2"
        android:textStyle="normal"
        android:textSize="14sp"
        android:textAlignment="center"
        android:background="@drawable/button_bg"
        android:textColor="@color/white"/> 

</RelativeLayout> 

Add this attribute to the bottom TextView 将此属性添加到底部的TextView

android:layout_alignParentRight="true"

This would be helpful then:) 这样会很有帮助:)

layout.xml layout.xml

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

<ImageSwitcher
    android:id="@+id/switcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inAnimation="@anim/fade_in" />

<TextView
    android:id="@+id/tv_1"
    android:layout_width="140dp"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/button_bg"
    android:gravity="center_horizontal|center_vertical"
    android:text="TV 1"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:textStyle="normal" />

<TextView
    android:id="@+id/tv_2"
    android:layout_width="140dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="15dp"
    android:background="@drawable/button_bg"
    android:gravity="center_horizontal|center_vertical"
    android:text="TV2"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:textStyle="normal" />

Remove gravity in both textviews. 删除两个文本视图中的重力。 And add align_parent_left and align_parent_top to first textview and align_parent_bottom and align_parent_right to second textview. 并将align_parent_left和align_parent_top添加到第一个文本视图,并将align_parent_bottom和align_parent_right添加到第二个文本视图。 Try below code: 试试下面的代码:

  <TextView  
    android:id="@+id/tv_1"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="15dp"
    android:layout_width="140dp" 
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="TV 1"
    android:textStyle="normal"
    android:textSize="14sp"
    android:textAlignment="center"
    android:background="@drawable/button_bg"
    android:textColor="@color/white"/>

   <TextView  
    android:id="@+id/tv_2"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="15dp"
    android:layout_width="140dp" 
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="TV2"
    android:textStyle="normal"
    android:textSize="14sp"
    android:textAlignment="center"
    android:background="@drawable/button_bg"
    android:textColor="@color/white"/> 

我没有尝试过,但是尝试将以下内容更改为marginRight或为marginLeft增加dp。

android:layout_marginLeft="15dp"

Try this 尝试这个

  android:layout_alignParentRight="true"

If you want to give some space between margin and @+id/tv_2 then you can adjust the below property: 如果要在margin和@ + id / tv_2之间一些间距,则可以调整以下属性:

android:layout_marginRight="XXdp"

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

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