Android TextView将文本左右对齐 [英] Android TextView Align text to Right and Left

查看:101
本文介绍了Android TextView将文本左右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个TextView,其中包含两段文本,一个与最左侧对齐,另一个与最右侧对齐.我可以在两个词之间加空格,但我想知道是否有更好的方法?

I'm trying to have a TextView that has two pieces of text, one aligned against the far left and one against the far right. I can put spaces between the two words, but I was wondering if there is a better technique?

<?xml version="1.0" encoding="utf-8"?>
<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/background_common"
    android:id="@+id/LinearLayout0123">

   <TextView
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:textColor="@color/Black"
        android:textStyle="bold"
        android:textSize="15dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/selector_social_facebook_twitter"
        android:text="Right Text                                  Left Text" />
</LinearLayout>

推荐答案

这是另一种方法:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_common">
    <TextView
        android:text="Left Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"/>
    <TextView
        android:text="Right Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"/>
</FrameLayout>

Layout_gravity告诉父项将子项放置在何处.在FrameLayout中,子项可以放到任何地方,并且不受其他子项的阻碍.如果有重叠,则最后添加的子项位于顶部.祝你好运!

Layout_gravity tells the parent where to put the child. In a FrameLayout, children can go anywhere and are unobstructed by other children. If there is overlap, the last child added is on top. Good luck!

这篇关于Android TextView将文本左右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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