Вопрос

In my app I have the following layout, and no matter what value I give the textview doesn't change its width. Can someone please help me in correcting this layout.

Here is the XML code:

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

I also get a warning near TableRow saying "This TableRow layout or its TableLayout parent is useless". Please guide me.

Thanks in advance.

Это было полезно?

Решение

Try this one Insted of table use Linear Layout with and set LAYOUT_WEIGHT property. This property shares the equal amount of space for text view and image button

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

Другие советы

try this....,you can add android:textSize="some value" which you want t o give

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

You might need to use

android:layout_weight

I am mentioning one example for your help, also make sure your every element has height and weight with it, to display everything correctly

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

Give the textsize using:

android:textSize="some value"

try to give size according to your requirement you have the width of the table row and textview set as match_parent hence it(textview) can't change it's size.

android:layout_width="50dp"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top