【问题标题】:TextView width in Table layout android表格布局android中的TextView宽度
【发布时间】:2014-06-02 19:41:13
【问题描述】:

在我的应用程序屏幕中,我正在使用表格布局。在表格行中,我有两个 textview。第二个 textview 的宽度总是超出屏幕宽度。我尝试了 wrap_content、match_parent 但没有得到如何解决这个问题。如何我将宽度设置为设备屏幕布局宽度?

xml代码-

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@color/even" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/tableLayoutDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/namerow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                >

                <TextView
                    android:id="@+id/nameText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:text="Name" />

                <TextView
                    android:id="@+id/dname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                     android:background="@android:color/black"
                    android:text="Name :"
                    android:textColor="#808080" />
            </TableRow>
 </TableLayout>
</LinearLayout>
</ScrollView>

即使在为 TextView 提供 100dp 宽度后,它也会离开屏幕

【问题讨论】:

  • 为什么需要表格行的权重总和,去掉并检查。
  • 那个我也试过了,还是一样
  • 尝试将线性布局宽度设置为 wrapcontent
  • @amj 试过但没用
  • @yuvaツ 修复您的 TableRow 宽度。 if wrap_content 表示如果 Child 长度增加 Parent 也会增加。

标签: android xml layout textview


【解决方案1】:

最好的问候

【讨论】:

    【解决方案2】:

    使用此代码:完美工作(用此代码替换您的代码并根据您的选择进行修改)

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/include1"
            android:layout_marginTop="20dp"
            android:gravity="top"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="30dp"
                android:orientation="horizontal"
                android:weightSum="100" >
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Name"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvfullname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Birthday"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvbirth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text=""
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Gender"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvgender"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text=""
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Mobile Number"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvmobile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text=""
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Email Id"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvemail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="sjdhgvfkasvsjdgfjsgdfjhgskdjfgakjsgdflaghdfljashdgfljadgsfjahg"
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="Location"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvlocation"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text=""
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_marginBottom="6dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:background="#e1e2dd" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text="City"
                    android:textColor="#454545"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/tvcity"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="left"
                    android:text=""
                    android:textColor="#454545"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>
      </LinearLayout>
    
    </ScrollView>
    

    看到这张图片:

    【讨论】:

    • 我也想要滚动视图,内容很多
    • 只使用了滚动视图...问题出在哪里
    【解决方案3】:

    可能是 Android 图形布局问题。我在 Android 视图设计中检查了您的代码。 TextView 字母不会超出布局。如果您有疑问将背景设置为文本视图,看看发生了什么?

    【讨论】:

    • 我已经更新了我的图像。将背景设置为文本视图后,它在屏幕宽度内。但文本仍然超出设备宽度
    • 有什么解决办法吗?
    猜你喜欢
    • 2011-05-02
    • 2012-01-24
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多