如何使我的TextView宽度为其父级的一半?

Kar*_*yan 9 java xml user-interface android textview

如果我想让我的TextView宽度完全等于其父级的宽度,我可以使用

android:layout_width="fill_parent"
Run Code Online (Sandbox Code Playgroud)

如果我想把它放到零件宽度的一半呢?或者,通常,设置相对于父宽度的宽度?

编辑:我正在使用相对布局

我的屏幕看起来像这样.

我的屏幕看起来像这样.

Chr*_*rry 14

快速而肮脏的方式是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000"
            />

    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

你必须将它包装在另一个容器中,然后只使用父项的一半权重.


DjH*_*orn 7

使用android:layout_weight="0.5"它将起作用linearLayout

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/feedback"
                    android:layout_width="wrap_content"
                    android:layout_weight="0.5"
                    android:textSize="15pt" />
Run Code Online (Sandbox Code Playgroud)