Android 布局控件居右

新手上路,请多包涵

问题描述

Android开发中如何实现一个控件居右的效果,如图所示,让控件2居右显示并占据空间。控件1的宽度自适应(占据除去控件2宽度后所有宽度)。
图片描述

问题出现的环境背景及自己尝试过哪些方法

设置项中需要用到这个布局,网上查询相关方法都不尽人意,自己也尝试了许多布局还是不行,希望能给一个示范布局代码。

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 6.6k
3 个回答

LinearLayout + android:layout_weight

使用constraintlayout

code

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="左左左左左左左左左左左左左左左左左左左左左左"
        app:layout_constraintEnd_toStartOf="@id/tv2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1111"
        app:layout_constraintBottom_toBottomOf="@id/tv1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv1" />
</androidx.constraintlayout.widget.ConstraintLayout>
新手上路,请多包涵

这不是很简单的事情么,控件2靠父布局RelativeLayout右对齐,控件1位于控件2左方。刚学习么?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题