質問

I have a LinearLayout with 3 or 4 views (Buttons and ImageButtons) inside, and I want that all of them have the same size to fit entirely the layout, like shown in the picture below example The same thing must be done either in horizontal and in vertical.

the xml of the LinearLayout is this:

 <LinearLayout
    android:id="@+id/discover_right_action_bar"
    android:layout_width="64dp"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_below="@id/top_navi_bar"
    android:background="@color/smart_dark_grey"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageButton
        android:id="@+id/home_ib_friend"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:saveEnabled="false"
        android:scaleType="center"
        android:src="@drawable/ic_action_select_all" />

    <ImageButton
        android:id="@+id/home_ib_camera"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:saveEnabled="false"
        android:scaleType="center"
        android:src="@drawable/ic_action_camera" />

    <ImageButton
        android:id="@+id/ib_prof_mon_meetings"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:saveEnabled="false"
        android:scaleType="centerInside"
        android:src="@drawable/clock" />
</LinearLayout>

Now I can only set or the size manually in dp, or set it to wrap_content, but than there would be space at the beginning and at the end of the layout.

I've tried something with the layout_weight attribute but I don't know well how to use it.

I've tried also with getting the real size of the layout (via ViewTreeObserver) and set the size to the internal children but it doesn't work because I couldn't get the real dimension of the layout.

役に立ちましたか?

解決 2

I resolve my problem: the solution above is working but I remove the weightSum attribute and it works if you don't know how many child views will be there. Their size will be adjusted dinamically and automatically to fit the whole parent layout.

他のヒント

if its vertical

 <LinearLayout
android:id="@+id/discover_right_action_bar"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:weightSum="3"
android:layout_below="@id/top_navi_bar"
android:background="@color/smart_dark_grey"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >

<ImageButton
    android:id="@+id/home_ib_friend"
    android:layout_width="60dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="center"
    android:src="@drawable/ic_action_select_all" />

<ImageButton
    android:id="@+id/home_ib_camera"
    android:layout_width="60dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="center"
    android:src="@drawable/ic_action_camera" />

<ImageButton
    android:id="@+id/ib_prof_mon_meetings"
    android:layout_width="60dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="centerInside"
    android:src="@drawable/clock" />

if horizontal:

 <LinearLayout
android:id="@+id/discover_right_action_bar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentRight="true"
android:weightSum="3"
android:layout_below="@id/top_navi_bar"
android:background="@color/smart_dark_grey"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >

<ImageButton
    android:id="@+id/home_ib_friend"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="center"
    android:src="@drawable/ic_action_select_all" />

<ImageButton
    android:id="@+id/home_ib_camera"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="center"
    android:src="@drawable/ic_action_camera" />

<ImageButton
    android:id="@+id/ib_prof_mon_meetings"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:saveEnabled="false"
    android:scaleType="centerInside"
    android:src="@drawable/clock" />

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top