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

时间:2013-03-10 09:17:01

标签: java android xml user-interface textview

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

android:layout_width="fill_parent"

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

编辑:我使用的是相对布局

我的屏幕看起来像这样。

My screen looks like this.

6 个答案:

答案 0 :(得分:13)

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

<?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>

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

答案 1 :(得分: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" />

答案 2 :(得分:1)

首先不要将“fill_parent”用作弃用的tearm,使用“match_parent”。

其次,您希望将半父TextView放在父母中?

当你使用RelativeLayout时,这个动作有点困难,试着从代码中做到这一点,有点像这样:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int myWidth = (int) (parentHeight * 0.5);
super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY),  
heightMeasureSpec);
}

参考:How to size an Android view based on its parent's dimensions

答案 3 :(得分:1)

如果你想使用RelativeLayout,那么最好的方法是在屏幕的中心放置一个虚拟视图,将第一个视图放在虚拟视图的右边,第二个放在虚拟视图的左边;

<?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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_contentt"
        android:background="#ff0000"
        android:layout_toLeftOf="@id/dummyView/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
         android:layout_toRightOf="@id/dummyView"/>
     <View
        android:id="+@id/dummyView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:layout_centerInParent="true"/>

</RelativeLayout>

答案 4 :(得分:0)

您可能需要设置父LinearLayout的weightSum,如下所示: -

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:padding="6dp"
    android:weightSum="2">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:layout_weight="1" />

</LinearLayout>

答案 5 :(得分:0)

使用线性布局包装TextView,如@ r-j和@djhacktorreborn所建议的,然后将整个内容放入相对布局中。