android - 如何在android相对布局中根据屏幕宽度为 View 设置可缩放宽度

标签 android android-layout android-relativelayout

在我的 Android 应用程序 Activity 的布局中,我需要一行文本在其右侧对齐,如下所示:

enter image description here

到目前为止,这是我的 XML:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_centerVertical="true"
                android:background="@android:color/darker_gray"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" Text "
                android:layout_alignRight="@id/line"/>


        </RelativeLayout>

基本上问题是线的宽度根据屏幕的宽度和文本的宽度而改变。它应该是屏幕宽度减去文本宽度。

在我的 XML 文件中,线条的宽度等于屏幕的宽度,文本覆盖在右侧对齐的线条上。这不是我想要的。 一个想法是让我的文本背景等同于 Activity 的背景。但是,我不确定这是否是最好的主意。

我想知道是否有任何其他方法可以解决 Android 设计中的此类问题。

最佳答案

您可以创建一个 layout_weight 并将您的 View 和 textView 包装在一个 linearlayout 中以启用使用权重属性

示例:

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

    <View
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="3dp"
        android:layout_gravity="center_vertical"
        android:background="@android:color/darker_gray" />

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

</LinearLayout>

关于android - 如何在android相对布局中根据屏幕宽度为 View 设置可缩放宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026434/

相关文章:

java - 如何验证俄语电子邮件?

java - ListView 适配器中的 ViewHolder 不起作用

android - 将一个 View 的垂直中心与另一个 View 的顶部对齐

安卓模拟器 : both soft input and hard keyboard input

android - Android中RelativeLayout中防止Scrollview与其他 View 重叠

android - 简单 Espresso 测试 "Looped for x iterations over 60sec"错误

java - 屏幕底部 1/3 处的对齐按钮

layout - React Native 是否有类似relativelayout 的东西?

android - 在有 id 的 relativeLayout 下设置一个没有 id 的 TextView 文本

android - 此 IP、站点或移动应用程序无权使用此 API key 。从 IP 地址收到的请求