布局权重在2.3.7 android中不起作用

布局权重在2.3.7 android中不起作用,android,formatting,textview,Android,Formatting,Textview,下面的代码试图在textview中的文本下方添加绿色下划线。这不适用于2.3.7手机,但适用于较新的设备。在旧设备中,文本下方没有绿色下划线。布局重量在旧设备上不起作用吗?有没有其他技巧可以达到同样的效果 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="

下面的代码试图在textview中的文本下方添加绿色下划线。这不适用于2.3.7手机,但适用于较新的设备。在旧设备中,文本下方没有绿色下划线。布局重量在旧设备上不起作用吗?有没有其他技巧可以达到同样的效果

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1" >

            <TextView
                android:id="@+id/onText"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.8"
                android:background="#F21861"
                android:fontFamily="sans-serif-condensed"
                android:gravity="center"
                android:text="off"
                android:textAllCaps="true"
                android:textColor="#454545" />

            <ImageView
                android:id="@+id/underline"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:background="#00a950"
                android:contentDescription="@null" />
        </LinearLayout>


Soubhit Puri提供的链接是更好的方法。

快速修复方法是将线性布局的高度设置为固定大小,例如25dp,然后将Textview的高度设置为22dp,将ImageView的高度设置为3dp

或者按照下面的代码,只需删除ImageView并将LinearLayout的背景设置为绿色,25dp,Textview高度设置为22dp

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:background="#00a950" >

            <TextView
                android:id="@+id/onText"
                android:layout_width="match_parent"
                android:layout_height="22dp"
                android:background="@android:color/white"
                android:fontFamily="sans-serif-condensed"
                android:gravity="center"
                android:text="off"
                android:textAllCaps="true"
                android:textColor="#454545" />
    </LinearLayout>


只是想知道,在
ImageView
中,android:layout\u height=“2dp”
想要实现什么?厚度?如果设置为0会发生什么?哦,对不起。应该将其设置为0dp,以便布局权重工作。我想有文本“顶部”和绿色下划线下面的文本。它在2.3.7手机上不工作。如果我把布局高度设置为2dp或其他什么,那么我可以看到下划线,否则就看不到。我已经更新了上面的代码。如果您不想使用ImageVIew,您仍然可以在
TextView
中为文本加下划线,而不使用ImageVIew。但下划线的颜色将是黑色。我不知道怎么换颜色<代码>textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG)我需要有绿色和灰色下划线,这取决于它是否被选中。文本的颜色也会相应地改变。谢索克明白了。你查过这个问题了吗?EditText也是如此。不过,你的方法似乎更简单、更严格。