I am making a bus schedule app. I have a lot of buttons in the main activity. My phone has a HD screen, so I made the buttons for that screen resolution.

My main activity

How can I make the positions of the buttons to fit every screen resolutions?

有帮助吗?

解决方案

If you are creating the buttons through xml layouts, then size them using dip (density independent pixels) as opposed to px (normal pixels). If you are pulling these images from resources, then you will have to have resources for all screen resolutions placed within the corresponding folders within the project structure (hdpi, mdpi, ldpi, etc.) - this would consume quite a bit of memory though.

其他提示

You can use GridView...It will allow you lots of button to equally distributed along the screen. You can follow these tutorials...

Android GridView Layout Tutorial

Android GridView example

Android Custom GridView Example

Android Custom GridView with Images and Text

You can use GridView for that purpose.

If you do not want to use GridView you can use linear with vertical orientation as main layout and linear layouts that contain buttons with "horizontal" orientation as rows. To make buttons fit same space on different resolutions you can use layout_weigth attribute.

Something like that

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

        <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

        <!-- yo can define as much buttons as you want -->

        </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

        <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

        <!-- yo can define as much buttons as you want -->

 </LinearLayout>    

<!-- and so on -->


</LinearLayout>

Defining layout this way may be not good for peformance so use it carefully

You can use grid view with 7 rows

GridView gridView = new GridView(context);
   gridView.setNumColumns(7);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top