【问题标题】:Android: Textview out of boundaryAndroid:Textview超出边界
【发布时间】:2013-05-02 07:30:43
【问题描述】:

我一直在尝试制作在 TableLayout 中包含 Textview 的布局。但是,textview 不适合布局。它走出了边界。将宽度更改为 fill_parent 或 wrap_content 似乎并不能解决问题。

请看这个screenshot

以下是xml代码:

<LinearLayout 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"
android:orientation="vertical"
tools:context=".ReportDetailActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerInside"
    android:src="@drawable/camera_icon" />

<TextView
    android:id="@+id/textViewCategory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:gravity="center"
    android:text="Pothole"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Comment : "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textViewComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Location : "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textViewLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aaaaaadsk jfshdfhsdkh fdsfdshfkdshfk hsdkhfk sdhkfhsd;kfhs"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date : "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textViewDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
</TableLayout>

【问题讨论】:

    标签: android xml layout textview


    【解决方案1】:

    尝试添加 layout_weight;

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Location : "
            android:layout_weight="1"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <TextView
            android:id="@+id/textViewLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="aaaaaadsk jfshdfhsdkh fdsfdshfkdshfk hsdkhfk sdhkfhsd;kfhs"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    

    【讨论】:

      【解决方案2】:

      尝试为 textview 提供固定宽度。这将防止文本离开屏幕:

      <TableRow
              android:id="@+id/tableRow2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" >
      
              <TextView
                  android:id="@+id/textView3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Location : "
                  android:textAppearance="?android:attr/textAppearanceLarge" />
      
              <TextView
                  android:id="@+id/textViewLocation"
                  android:layout_width="200dp"
                  android:layout_height="wrap_content"
                  android:text="aaaaaadsk jfshdfhsdkh fdsfdshfkdshfk hsdkhfk sdhkfhsd;kfhs"
                  android:textAppearance="?android:attr/textAppearanceLarge" />
          </TableRow>
      

      【讨论】:

        【解决方案3】:

        使用这个

        <LinearLayout 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"
            android:orientation="vertical"
            tools:context=".ReportDetailActivity" >
        
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:src="@drawable/camera_icon" />
        
            <TextView
                android:id="@+id/textViewCategory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="Pothole"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
        
                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
        
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Comment : "
                        android:textAppearance="?android:attr/textAppearanceLarge" />
        
                    <TextView
                        android:id="@+id/textViewComment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textAppearance="?android:attr/textAppearanceLarge" />
                </TableRow>
        
                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
        
                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Location : "
                        android:textAppearance="?android:attr/textAppearanceLarge" />
        
                    <TextView
                        android:id="@+id/textViewLocation"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="aaaaaadsk jfshdfhsdkh fdsfdshfkdshfk hsdkhfk sdhkfhsd;kfhs"
                        android:textAppearance="?android:attr/textAppearanceLarge" />
                </TableRow>
        
                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
        
                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Date : "
                        android:textAppearance="?android:attr/textAppearanceLarge" />
        
                    <TextView
                        android:id="@+id/textViewDate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textAppearance="?android:attr/textAppearanceLarge" />
                </TableRow>
            </TableLayout>
            </LinearLayout>
        

        【讨论】:

          【解决方案4】:

          尝试将 fix layout_width 设置为您的 textview 并在需要时设置 maxlines 或 minlines 属性。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-24
            • 2016-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-17
            • 1970-01-01
            相关资源
            最近更新 更多