Как сделать так, чтобы вложенные линейные макеты занимали половину родительского макета

1

Внутри моего внешнего менеджера макетов у меня есть это:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:layout_margin="2dp"
    android:id="@+id/linearlayout1" android:layout_weight="1">


    <com.me.android.ui.ImageView
        android:id="@+id/artistimage" android:layout_height="177dp"
        android:layout_width="wrap_content" android:scaleType="centerCrop"
        android:layout_margin="5dp" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_alignParentTop="true"
        android:layout_weight="1">

        <TextView android:layout_height="wrap_content"
            android:textSize="24dp" android:paddingBottom="5dp"
            android:textStyle="bold" android:paddingLeft="5dp"
            android:paddingTop="5dp" android:layout_width="fill_parent"
            android:textColor="@color/redtext" android:id="@+id/artistname" />

        <ToggleButton android:id="@+id/artistfav"
            android:layout_alignParentRight="true" android:background="@drawable/favbutton"
            android:textOff=" " android:textOn=" " android:layout_marginTop="5dp"
            android:layout_width="fill_parent" android:layout_height="30dp"
            android:layout_marginRight="10dp" android:text="Add to Favorites"
            android:scaleType="fitXY" />

    </LinearLayout>

</LinearLayout>

Имейте в виду, что эти два linearlayouts вложены внутри самого внешнего диспетчера макетов, который также имеет значение linearlayout. Я хочу просто иметь каждый из linearlayout, который я справился здесь, чтобы заняться ровно половиной родителя. Я думал, что могу просто установить вес каждого менеджера linearlayout равным 1, но не кубик. Прямо сейчас изображение, например, занимает около 70% родительского...

Что я делаю неправильно?

EDIT:

ОК, я изменил его на это, и изображение заняло половину макета. Ницца! Но текст и кнопка исчезли.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:layout_margin="2dp"
    android:id="@+id/linearlayout1">


    <com.me.android.ui.ImageView
        android:id="@+id/artistimage" android:layout_height="177dp"
        android:layout_width="fill_parent" android:scaleType="centerCrop"
        android:layout_margin="5dp" android:layout_weight="1" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="1">

        <TextView android:layout_height="wrap_content"
            android:textSize="24dp" android:paddingBottom="5dp"
            android:textStyle="bold" android:paddingLeft="5dp"
            android:paddingTop="5dp" android:layout_width="0dip"
            android:textColor="@color/redtext" android:id="@+id/artistname" />

        <ToggleButton android:id="@+id/artistfav"
            android:layout_alignParentRight="true" android:background="@drawable/favbutton"
            android:textOff=" " android:textOn=" " android:layout_marginTop="5dp"
            android:layout_width="0dip" android:layout_height="30dp"
            android:layout_marginRight="10dp" android:text="Add to Favorites"
            android:scaleType="fitXY" />

    </LinearLayout>
</LinearLayout>
Теги:

3 ответа

4
Лучший ответ

Поскольку внешний LinearLayout равен horizontal, установите android:layout_width его дочерних элементов на 0dip. Тогда только android:layout_weight будет контролировать размер.

  • 0
    Спасибо Марк. Просто чтобы убедиться, что мы ясны. Эти два линейных макета вложены в еще один линейный макет (который я не скопировал в своем OP) ...
  • 0
    Я в замешательстве ... не могу заставить его работать. установить layout_width дочернего linearlayout (вертикальный) в 0dip ? или установить для imageview значение 0dip ? Перепробовал все комбинации, не могу заставить его работать.
Показать ещё 2 комментария
0

вы указали ширину текста и кнопку на 0, чтобы объяснить, почему они не видны

0

Ну, это то, что сработало для меня:

    <com.me.android.ui.ImageView
        android:id="@+id/artistimage" android:layout_height="150dp"
        android:layout_width="fill_parent" android:scaleType="centerCrop"
        android:layout_margin="5dp" android:layout_weight="1" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="1" >

        <TextView android:layout_height="wrap_content"
            android:textSize="22dp" android:paddingBottom="5dp"
            android:textStyle="bold" android:paddingLeft="5dp"
            android:paddingTop="5dp" android:layout_width="fill_parent"
            android:textColor="@color/redtext" android:id="@+id/artistname"/>

        <ToggleButton android:id="@+id/artistfav"
            android:layout_alignParentRight="true" android:background="@drawable/favbutton"
            android:textOff=" " android:textOn=" " android:layout_marginTop="5dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginRight="10dp" 
             />

    </LinearLayout>
</LinearLayout>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню