固定TextView的宽度

固定TextView的宽度

本文介绍了固定TextView的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有以下LinearLayout.我将其用于ListView中的一行.

I have the following LinearLayout below. I use it for a row in a ListView.

我已将Textview的权重设置为1,因此它们的宽度都应相等.

I have set the Textview's weight to 1 so they should all be of equal width.

问题在于,当某些文本视图中包含更多字符时,文本视图的宽度会发生变化.这会对ListView的显示方式产生整体影响,因为每一行中的数据均未正确排列. (我的标题在ListView上方).

The problem is when some of the textviews have more characters in, the width of the textview changes. This has an overall effect on how the ListView appears, as in each row the data in not lined up correctly. (i have headings above the ListView).

当某些TextView中包含更多文本时,文本的两边仍然有空间,并且Textview会变大,这是我不想要的,因为我已将权重设置为1.

when some of the TextViews have more text in them, there is still space either side of the text and the textview just gets bigger, which i don't want as i've set the weights to 1 each.

有没有一种方法可以固定每个textview的宽度,以便它不会根据其中的文本长度而改变?

Is there a way of fixing the width of each textview so it doesn't change according to the length of text in it?

预先感谢

马特

<?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"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/rowstarttimeweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2" />

    <TextView
        android:id="@+id/rowplannedcallsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/rowncrsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/rowqasweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/rowplannedhoursweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:33"
        android:layout_weight="1"
        android:gravity="right"/>

    <TextView
        android:id="@+id/rowactualcallsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1"
        android:gravity="right"/>

    <TextView
        android:id="@+id/rowactualhourssweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:01"
        android:layout_weight="1"
        android:gravity="right"/>


</LinearLayout>

推荐答案

一个小的更改即可解决您的问题

One small change and your problem is solved

将所有TextView的宽度从wrap_content更改为0dp

change width of all TextView from wrap_content to 0dp

android:layout_width="0dp"

只要将高度/宽度设置为0dp,它将按照android:layout_weight

Whenever you will set height/width to 0dp, it will manage height/width of view as per android:layout_weight

这篇关于固定TextView的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:44