【问题标题】:TextView showing very small in the screenTextView 在屏幕上显示非常小
【发布时间】:2014-02-21 08:10:46
【问题描述】:

在我的应用程序中,我有以下布局,无论我给 textview 赋予什么值都不会改变它的宽度。有人可以帮我纠正这个布局吗?

这是 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginTop="15dp"
android:descendantFocusability="blocksDescendants" >

<TableRow
    android:layout_width="match_parent"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/rec_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/fill_rece"
        android:ems="10"
        android:focusable="false"
        android:padding="10dp"
        android:textColor="#FFFFFF" />

    <ImageButton
        android:id="@+id/btn_rec_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/content"
        android:src="@drawable/ipad_postcare_landscape_from" />
</TableRow>

</TableLayout>

我还在 TableRow 附近收到警告说“此 TableRow 布局或其 TableLayout 父级无用”。请指导我。

提前致谢。

【问题讨论】:

  • 您是否尝试过为 TextView 的“layout_width”属性使用“fill_parent”?
  • @user1406716 fill_parent 已弃用,使用 match_parent 是正确的。
  • 删除android:ems="10"
  • 关于无用的警告是因为您在TableLayout 中只有1 个TableRow。添加更多TableRow,警告将消失。或者只是使用其他布局(例如RelativeLayout

标签: android xml textview android-tablelayout


【解决方案1】:

试试这个 Insted 表格使用线性布局并设置LAYOUT_WEIGHT 属性。此属性为文本视图和图像按钮共享等量的空间

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

<TextView
    android:id="@+id/rec_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/fill_rece"
    android:focusable="false"
    android:padding="10dp"
    android:textColor="#FFFFFF" />

<ImageButton
    android:id="@+id/btn_rec_delete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
     android:layout_weight="1"
    android:contentDescription="@string/content"
    android:src="@drawable/ipad_postcare_landscape_from" />
</LinearLayout>

【讨论】:

  • 试过了。宽度没有变化。
  • @user2688158 您是否删除了表格布局......只需在您的 XML 中替换此代码。
  • 是的,它是一样的......只是在 LinearLayout 中有 android:layout_height="?android:attr/listPreferredItemHeight" 和 android:descendantFocusability="blocksDescendants"
【解决方案2】:

试试这个......,你可以添加你想给的 android:textSize="some value"

    <TextView
        android:id="@+id/rec_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:padding="10dp"
        android:text="hllo"
        android:textSize="25dp" />

【讨论】:

    【解决方案3】:

    您可能需要使用

    android:layout_weight
    

    我举一个例子来帮助你,还要确保你的每个元素都有高度和重量,以正确显示所有内容

    <LinearLayout  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/layout_border"
    android:weightSum="1.0"
    android:id="@+id/r1c1r2">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:textIsSelectable="true"
                android:id="@+id/key"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:textIsSelectable="true"
                android:id="@+id/key2" />
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      使用以下方式给出文本大小:

      android:textSize="some value"
      

      【讨论】:

        【解决方案5】:

        尝试根据您的要求提供大小,您将表格行的宽度和 textview 设置为 match_parent,因此它(textview)无法更改它的大小。

        android:layout_width="50dp"
        

        【讨论】:

        • 我已经尝试了所有可能的值..不改变宽度
        • 我认为问题出在你的 android:background="@drawable/fill_rece" 行。你必须在 xml 文件中定义宽度大小。尝试删除可绘制文件并更改宽度..它有效!
        • 是的,这是可能的,我会尝试获得更好的可绘制对象,看看它是否有任何不同。无论如何,谢谢。
        • 我现在已经支持它了..我会尝试使用新的可绘制对象,然后如果它工作正常则接受作为答案。感谢您的建议。
        • 你在textview的背景中显示什么......只是好奇
        猜你喜欢
        • 2014-02-07
        • 2022-01-03
        • 2018-02-07
        • 2016-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        相关资源
        最近更新 更多