开发者

Android: even spacing between view aligned vertically

My layout have 4 EditText views aligned vertically. but there is empty spacing after the last EditText. I want to align these views evenly so that the spacing b/w, before and after开发者_C百科 the views is same. How to achieve this?

--nehatha


You can wrap every of your EditText to a FrameLayout with equal layout weight and set the Gravity of those frame layouts to center (or layout_gravity of EditTexts).

Edited:
Well, my initial solution leaves less space at top and bottom than between the edit texts. But this one works fine: simply add FrameLayout between each view and at top and bottom with layout_weight="1":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
    ></FrameLayout>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    ></EditText>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
    >
    </FrameLayout>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    ></EditText>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
    >
    </FrameLayout>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    ></EditText>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
    >
    </FrameLayout>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    ></EditText>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
    >
    </FrameLayout>
</LinearLayout>


if you are using xml, then in all EditText add this tag,

android:layoutMargin = "5dip"

and in your parent layout (i ll assume its LinearLayout )

android:layoutHeight = "wrap_content"

Updated

adding hard coded values does't harm your code, since "dip" and "dp" are all units which get adapted to different screen resolutions. it has zero affect evn if the screen resolution changes..

chek this.. http://developer.android.com/resources/samples/MultiResolution/index.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜