Как увеличить или уменьшить размер текстовых просмотров Android в зависимости от его содержимого? - PullRequest
0 голосов
/ 20 марта 2012

Ширина + wrap_content прекрасно работает с другими однострочными TextViews в моем макете. Однако мои многострочные TextViews не будут ни уменьшаться, ни увеличиваться, чтобы адаптироваться к числам в нем. В идеале я хотел бы, чтобы представление обернуло его высоту и ширину, чтобы соответствовать содержанию в нем.

TextViews 1-10 (textViews, расположенные в верхней части каждой кнопки) имеют одну линию и работают так, как я хочу. Они расширяются и сжимаются в зависимости от их размера (переход к содержимому работает как следует).

TextViews 11-20 имеют фиксированный размер и не будут сжиматься или расширяться, несмотря на наличие идентичного кода, отличного от многоканального. (они используют \ n в своих строках). Они должны состоять из двух чисел от 0 до 100, расположенных вертикально.

Интерфейс

enter image description here

<?xml version="1.0" encoding="utf-8"?>

            <TextView android:id="@+id/textView1" android:layout_alignLeft="@+id/imageButton1" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_below="@+id/imageButton1" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/textView5" android:layout_alignLeft="@+id/imageButton5" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_above="@+id/imageButton5" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/textView7" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_above="@+id/imageButton7" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
    <TextView android:id="@+id/textView8" android:layout_alignBottom="@+id/textView3" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
    <TextView android:id="@+id/textView9" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_below="@+id/imageButton9" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
    <TextView android:id="@+id/textView10" android:layout_alignLeft="@+id/imageButton10" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_below="@+id/imageButton10" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/textView6" android:layout_alignBottom="@+id/textView7" android:layout_alignLeft="@+id/imageButton6" android:background="#000000" android:layout_width="wrap_content" android:layout_alignBaseline="@+id/textView7" android:text="0/0/0" android:visibility="invisible" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/textView4" android:layout_alignBottom="@+id/textView5" android:layout_alignLeft="@+id/imageButton4" android:background="#000000" android:layout_width="wrap_content" android:layout_alignBaseline="@+id/textView5" android:text="0/0/0" android:visibility="invisible" android:layout_height="wrap_content" android:layout_marginLeft="-10dp"></TextView>
    <TextView android:id="@+id/textView3" android:layout_alignLeft="@+id/imageButton3" android:background="#000000" android:layout_width="wrap_content" android:text="0/0/0" android:layout_above="@+id/imageButton3" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content" android:layout_marginLeft="-10dp"></TextView>
    <TextView android:id="@+id/textView2" android:layout_alignBottom="@+id/textView1" android:layout_alignLeft="@+id/imageButton2" android:background="#000000" android:layout_width="wrap_content" android:layout_alignBaseline="@+id/textView1" android:text="0/0/0" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="invisible" android:layout_height="wrap_content" android:layout_marginLeft="-10dp"></TextView>


    <TextView android:id="@+id/textView11" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/imageButton1" android:layout_toRightOf="@+id/nextHandButton" android:layout_above="@+id/textView1"></TextView>
    <TextView android:id="@+id/textView12" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/imageButton2" android:layout_above="@+id/textView2" android:layout_alignLeft="@+id/textView2"></TextView>
    <TextView android:id="@+id/textView13" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/imageButton3" android:layout_alignRight="@+id/textView12" android:layout_above="@+id/imageView4" android:layout_alignLeft="@+id/textView3"></TextView>
    <TextView android:id="@+id/textView14" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/textView4" android:layout_alignTop="@+id/imageButton4" android:layout_toLeftOf="@+id/imageButton4"></TextView>
    <TextView android:id="@+id/textView15" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/textView5" android:layout_toLeftOf="@+id/imageButton5" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/textView11"></TextView>
    <TextView android:id="@+id/textView16" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/imageButton6" android:layout_alignLeft="@+id/imageView6" android:layout_alignParentBottom="true" android:layout_toLeftOf="@+id/nextHandButton"></TextView>
    <TextView android:id="@+id/textView17" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/imageButton7" android:layout_toRightOf="@+id/imageButton7" android:layout_alignParentBottom="true" android:layout_toLeftOf="@+id/textView6" android:layout_marginRight="50dp"></TextView>
    <TextView android:id="@+id/textView18" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/textView8" android:layout_toRightOf="@+id/imageButton8" android:layout_alignBottom="@+id/imageView7" android:layout_alignRight="@+id/textView17" android:maxLines="2"></TextView>
    <TextView android:id="@+id/textView19" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageButton9" android:layout_alignBottom="@+id/imageButton9" android:layout_alignRight="@+id/textView18"></TextView>
    <TextView android:id="@+id/textView20" android:visibility="invisible" android:background="#000000" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignLeft="@+id/imageView10" android:layout_alignBottom="@+id/imageButton10" android:layout_alignRight="@+id/textView16" android:layout_height="wrap_content" android:maxLines="2"></TextView>
</RelativeLayout>

1 Ответ

1 голос
/ 20 марта 2012

То, что здесь произошло, было следствием использования графического макета затмения и перетаскивания с относительным макетом андроида вместо того, чтобы самому вводить относительные ограничения.Существовали ненужные ограничения, такие как:

android:layout_alignRight="@+id/textView18"

Приведенное выше ограничение остановило бы расширение представления вправо.Эти ограничения не позволили свойствам содержимого переноса по ширине или высоте вступить в силу.Если вы сталкиваетесь с подобной проблемой, внимательно изучите свой код на предмет относительных ограничений макета, которые могут перекрывать вашу способность «Виды» расширяться или уменьшаться в нужном вам направлении.

...