Android:将几个按钮缩放到屏幕大小

时间:2014-06-12 07:49:14

标签: android button adt scale

我遇到了问题。在我的应用程序中,我有一些(3-4)按钮,想要自动缩放到全屏尺寸。 (不只是一个按钮,而是全部在一起)。目前我正在为我的应用程序使用LinearLayout。有可能用它吗? 抱歉我的英语不好。

问候Nils

游戏“不要点击白色”的屏幕与我喜欢的类似:

5 个答案:

答案 0 :(得分:2)

您可以使用Weight概念。您的按钮在所有屏幕中看起来都一样。

首先将屏幕划分为2个水平部分,每个部分的宽度重量为50 50,高度与父级相匹配。然后为每个部件添加3个按钮,高度为33.33,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
             android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
             android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button4"
             android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
             android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

        <Button
            android:id="@+id/button6"
             android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="33.33"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

答案 1 :(得分:1)

丑陋的解决方案是垂直使用3个加权LinearLayouts(具有相同的权重),同时使用加权水平LinearLayout水平地实现相同的效果。

或者你可以使用GridView以更优雅的方式实现类似的效果:http://developer.android.com/guide/topics/ui/layout/gridview.html

答案 2 :(得分:0)

将Button的宽度设置为match_parent。然后它将缩放以适合父母。

答案 3 :(得分:0)

是..YOu可以使用LinearLayout来完成。你必须设置android:layout_weight =“”到按钮..这取决于水平和垂直按钮的数量,以给予权重..

答案 4 :(得分:0)

使用此Xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" 
        android:weightSum="2">

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Button"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Button"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Button"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Button"
            android:layout_weight="1" />
    </LinearLayout>


</LinearLayout>
相关问题