0

У меня есть простой XML, который я пытаюсь раздуть и борется с макетом. То, что я хочу достичь, довольно просто (или, как я думал), поэтому я уверен, что я ошибаюсь. В основном у меня есть три элемента: 2 TextViews и Imageview. Я хочу, чтобы два текстовых поля располагались друг над другом с изображением справа от них, выровненным с правой стороны, и его представление соответствовало размеру изображения. Как это:Настройка макетов по размеру их содержимого

Text 1      |==| 
Text 2 would be below text 1 |==| 

Heres мой XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <LinearLayout 

     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/entry1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="@dimen/tab_host_default_height" 
      android:text="@string/test_string" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/entry2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 

     <ImageButton 
      android:id="@+id/entry3" 
      style="@style/ImageButtonBDTheme" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/activated_background_holo_light" 
      android:contentDescription="@string/click_for_repair_report" 
      android:paddingRight="5dp" 
      android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 

Проблема заключается в том, что я не могу получить изображение, чтобы увидеть без жесткого кодирования размера LinearLayout, содержащего два текстовых поля.

Любая помощь приветствуется!

ответ

1
// try this 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/entry_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:minHeight="?android:attr/listPreferredItemHeight" 
       android:paddingRight="?android:attr/scrollbarSize" 
       android:gravity="center"> 

    <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

     <TextView 
       android:id="@+id/entry1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ellipsize="marquee" 
       android:fadingEdge="horizontal" 
       android:paddingLeft="@dimen/tab_host_default_height" 
       android:text="@string/test_string" 
       android:textStyle="bold" /> 

     <TextView 
       android:id="@+id/entry2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ellipsize="marquee" 
       android:fadingEdge="horizontal" 
       android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical"> 

     <ImageButton 
       android:id="@+id/entry3" 
       style="@style/ImageButtonBDTheme" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/activated_background_holo_light" 
       android:contentDescription="@string/click_for_repair_report" 
       android:paddingRight="5dp" 
       android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 
+0

У вас есть это ! Спасибо за быстрый ответ :) – xceph

+0

Не могли бы вы объяснить, как это работает? Я бы подумал установить вес в одном, но не другой мог бы отображать только этот элемент. – xceph

+0

какой бы размер ни был необходим, чтобы показать изображение, которое потребуется, и остаток размера используется в макете textview. –

1

Попробуйте это ..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <LinearLayout 
     android:layout_width="0dp"     //changes here 
     android:layout_weight="0.6"     //changes here 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/entry1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="@dimen/tab_host_default_height" 
      android:text="@string/test_string" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/entry2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp"    //changes here 
     android:layout_weight="0.4"    //changes here 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 

     <ImageButton 
      android:id="@+id/entry3" 
      style="@style/ImageButtonBDTheme" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/activated_background_holo_light" 
      android:contentDescription="@string/click_for_repair_report" 
      android:paddingRight="5dp" 
      android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 
+0

Спасибо, но установка веса не соответствует его размеру изображения. Я пытаюсь сделать макет, содержащий вид точного размера изображения + дополнение, с LinearLayout, содержащим текст, используя остальное – xceph

+0

@xceph, тогда укажите ширину макета изображения как wrap_content и этот вес макета текста как 1 – Hariharan

1

Всегда используйте Relative Layout, она требует меньшего нет. кода по сравнению с другими макетами.

Попробуйте ниже, я думаю, это то, что вы хотите, установите Padding/Margin в соответствии с вашим требованием.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <TextView 
     android:id="@+id/entry1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:gravity="left" 
     android:paddingLeft="@dimen/tab_host_default_height" 
     android:text="@string/test_string" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/entry2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/entry1" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:gravity="left" 
     android:paddingLeft="5dp" 
     android:paddingTop="5dp" 
     android:text="@string/test_string" /> 

    <ImageButton 
     android:id="@+id/entry3" 
     style="@style/ImageButtonBDTheme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/ic_launcher" 
     android:contentDescription="@string/click_for_repair_report" 
     android:paddingRight="5dp" 
     android:src="@drawable/entry_link" /> 

</RelativeLayout> 
+0

Спасибо, это тоже сработало и действительно намного меньше. – xceph

+0

В этом случае вы можете подтвердить свой ответ. –

+0

У тебя это чувак – xceph

Смежные вопросы