首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Android布局中对齐左右边缘的文本视图

在Android布局中对齐左右边缘的文本视图
EN

Stack Overflow用户
提问于 2010-01-20 13:34:16
回答 9查看 196.4K关注 0票数 166

我开始使用Android了。我在进行一个简单的布局时遇到了麻烦。

我想使用LinearLayout将两个TextViews放置在一行中。一个TextView在左边,另一个在右边(类似于CSS中的float: left,float:right )。

这是可能的吗,或者我需要使用不同的ViewGroup或进一步的布局嵌套来完成它?

这是我到目前为止所知道的:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:padding="10sp">
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/>
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end"
    android:text="somestringontheright" android:layout_width="wrap_content"/>
</LinearLayout>
EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-01-20 14:38:16

RelativeLayoutlayout_alignParentLeftlayout_alignParentRight一起使用

代码语言:javascript
复制
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="10dp">

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:id="@+id/mytextview1"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:id="@+id/mytextview2"/>

</RelativeLayout>

还有,you should probably be using dip (or dp) rather than sp in your layoutsp反映文本设置以及屏幕密度,因此它们通常仅用于调整文本项的大小。

票数 299
EN

Stack Overflow用户

发布于 2010-01-20 15:11:00

你可以使用重力属性来“浮动”视图。

代码语言:javascript
复制
<?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">

    <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal">

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:gravity="left"
            android:layout_weight="1"
            android:text="Left Aligned"
            />

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="1"
            android:text="Right Aligned"
            />
    </LinearLayout>

</LinearLayout>
票数 90
EN

Stack Overflow用户

发布于 2018-03-07 21:22:57

这可以使用LinearLayout完成(比相对布局选项更少的开销和更多的控制)。让第二个视图留出剩余空间,这样gravity就可以工作了。已测试回API 16。

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned left" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="Aligned right" />
</LinearLayout> 

如果要限制第一个文本视图的大小,请执行以下操作:

根据需要调整权重。相对布局不允许您像这样设置百分比权重,只能设置其中一个视图的固定dp

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Aligned left but too long and would squash the other view" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="Aligned right" />
</LinearLayout>
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2099249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档