Как иметь масштабируемую ширину в зависимости от ширины экрана для представления в относительном макете Android

StackOverflow https://stackoverflow.com//questions/25026434

Вопрос

В макете действия моего приложения для Android мне нужно иметь строку с текстом, выровненным по правой стороне, как показано ниже:

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-файле ширина линии равна ширине экрана, а текст перекрывает линию, выровненную по правому краю.Это не то, чего я хотел.Одна из идей — сделать фон моего текста эквивалентным фону действия.Однако я не уверен, что это лучшая идея.

Мне интересно, есть ли другой способ решения таких проблем в дизайне Android.

Это было полезно?

Решение

вы можете создать layout_weight и оберните свой взгляд и 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>

Другие советы

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text=" Text " />

<View
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/textView1"
    android:background="@android:color/darker_gray" />
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top