首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >直线上textViews与底边对齐的问题

直线上textViews与底边对齐的问题
EN

Stack Overflow用户
提问于 2019-12-26 11:44:42
回答 5查看 41关注 0票数 1

我是android的新手,在开始布局的时候,我只是把两个textViews的底部边缘固定在一条直线上,而没有一些硬编码(比如定义固定的sp值来对齐两者)。这是我的密码:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/time_text_view"
        android:text="2:10"
        android:textStyle="bold"
        android:textSize="40sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/time_text_view"
        android:text="PM"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

下面是编译后的样子:

我想让'PM‘和时间在相同的基础上,而不是在布局的基础上.

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2019-12-26 11:51:25

您可以使用ConstraintLayout,如:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:10"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PM"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBaseline_toBaselineOf="@+id/time_text_view"
        app:layout_constraintStart_toEndOf="@+id/time_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>

或使用RelativeLayout作为:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:10"
        android:textSize="40sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/time_text_view"
        android:layout_toEndOf="@+id/time_text_view"
        android:text="PM"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>
票数 2
EN

Stack Overflow用户

发布于 2019-12-26 11:51:05

您可以使用ConstraintLayout轻松地通过分配:

  • 左/启动"PM“的水平约束到右侧/ "2:10”
  • 底部的末尾"PM“到”2:10“底部的垂直约束

http://schemas.android.com/apk/res/android“xmlns:app="http://schemas.android.com/apk/res-auto”xmlns:tools="http://schemas.android.com/tools“android:layout_width="wrap_content”android:layout_height="wrap_content“tools:context=".MainActivity">

票数 1
EN

Stack Overflow用户

发布于 2019-12-26 11:52:14

更新代码

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:10"
        android:textStyle="bold"
        android:textSize="40sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PM"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

如果这有用的话请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59487985

复制
相关文章

相似问题

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