简体   繁体   中英

How can i resize the layout and scrollview according to screen resolution

I set the layout and scroll view for the screen resolution 480x800. How can I be compatible with other screen resolution? Here is my code. Please help me.

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="vertical" >
<ScrollView android:id="@+id/ScrollView01"
 android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#86C3C6" >
<TextView
        android:id="@+id/tvHistory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#86C3C6"
        android:gravity="right"
        android:focusable="true"
        android:text=""
        android:textColor="#FFFFFF" />
 </ScrollView>
</RelativeLayout>

Please help me to set the layout,scroll view and textsize through program

First of all, Android does not support the concept of "screen resolution." There's screen size and screen (or pixel) density. Since you're already using density-independent pixels (dp), I imagine that your question is about supporting different screen sizes.

The way to support other screen sizes is to have size-specific resource directories. For instance, in addition to res/layout (where you probably now have the above layout file), you could have res/layout-large to support large screens. The details about this are described in the Guide topic Supporting Multiple Screens and the pages that it links to.

You might also consider not specifying an exact size for your views, but rather let them fill the parent or wrap their content. If that doesn't work for your layout and you need to specify view sizes, you can make the size a variable and define the size in the res/values , res/values-large , etc. folders. For instance, in res/layout/main.xml you could have your layout:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="@dimen/relative_layout_height"
    android:orientation="vertical" >
    <ScrollView android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/scroll_view_height"
        android:background="#86C3C6" >
        <TextView
            android:id="@+id/tvHistory"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#86C3C6"
            android:gravity="right"
            android:focusable="true"
            android:text=""
            android:textColor="#FFFFFF" />
    </ScrollView>
</RelativeLayout>

Then in each values folder you can have a dimens.xml file with the relevant dimensions given appropriate values. In res/values/dimens.xml (for medium-size screens):

<dimen name="relative_layout_height">150dp</dimen>
<dimen name="scroll_view_height">200dp</dimen>

While in res/values-large/dimens.xml you would have:

<dimen name="relative_layout_height">250dp</dimen>
<dimen name="scroll_view_height">300dp</dimen>

That way, you would only need one layout and it would be parameterized by dimension values that would vary by screen size.

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" //would fill what ever size it wud be.
android:orientation="vertical" >
  <ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" //would fill what ever size it wud be.
    android:background="#86C3C6" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM