开发者

Allow layout to occupy a % of screen space?

开发者 https://www.devze.com 2023-03-12 06:28 出处:网络
Is it possible to allow a layout to occupy a percentage of screen space? I\'ve got 2 layouts currently one takes up about 130dip height and that\'s fine for most screens but when开发者_如何转开发 I s

Is it possible to allow a layout to occupy a percentage of screen space?

I've got 2 layouts currently one takes up about 130dip height and that's fine for most screens but when开发者_如何转开发 I switch to landscape the other linearlayout becomes barely visible on certain screen sizes. How would I insure that each of these are given equal screen space?


Set for each layout the same value for android:layout_weight.

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#FF0000" />
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#0000FF" />
</LinearLayout>


use android:layout_weight, here's the docs with an exmaple:

Here's a stripped down example:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
</LinearLayout>

That will make each of the 2 inner LinearLayouts take up half the screen.


set android:layout_weight = "1" in each of the items that you want to take equal space. This will make it work with whatever size of screen.

setting android:layout_width to be the same value for all will NOT work for all screens sizes.

... however using layout_weight will automatically distribute each equally regardless of screen size

0

精彩评论

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