jsmith
jsmith

Reputation: 4897

How to: Layout fixed size components with justified spacing

When I have 1, 2 or 3 components I know how to get them to space properly. But now I have a view that will have 4, 5, ... components in it. Rather than increasing the size of the components (via weight), I need them to remain a fixed size. So... is there any way to lay these out with justified spacing (evenly spaced within the view)?

Thanks,

J

Upvotes: 2

Views: 2311

Answers (2)

jsmith
jsmith

Reputation: 4897

A similar work around that I've figured out is to add spacer view components. Again, this is not ideal, but it works. If there is anything better, please let me know.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <biz.mycompany.mycomponent
        android:layout_width="@dimen/screen_view_w"
        android:layout_height="@dimen/screen_view_h"
        />
    <View
        android:layout_width="0px"
        android:layout_height="@dimen/main_screen_button_h"
        android:layout_weight="1"
        />
    <biz.mycompany.mycomponent2
        android:layout_width="@dimen/screen_view_w"
        android:layout_height="@dimen/screen_view_h"
        />
    <View
        android:layout_width="0px"
        android:layout_height="@dimen/screen_view_h"
        android:layout_weight="1"
        />
    <biz.mycompany.mycomponent3
        android:layout_width="@dimen/screen_view_w"
        android:layout_height="@dimen/screen_view_h"
        />
</LinearLayout>

Upvotes: 2

Kevin Coppock
Kevin Coppock

Reputation: 134664

Something like the following would work, but is not necessarily the most efficient way. What type of components are you wanting to insert? Basically, I created a horizontal LinearLayout, with child LinearLayouts with even weights. I set the gravity of these inner LinearLayouts to center their children within, so then your fixed width views go within at their proper sizes, centered evenly within.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_1"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_2"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_3"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_4"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_5"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        >
        <View 
             android:id="@+id/fixed_view_6"
             android:layout_width="30dp"
             android:layout_height="50dp"
             />
</LinearLayout>

Upvotes: 1

Related Questions