如何使我的按钮或布局适应任何设备?

时间:2017-07-10 15:46:41

标签: android xml sdk



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ilafedoseev.calculatorkotlin.MainActivity"
    android:background="@drawable/gradient">

    <TextView
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"
        android:gravity="right"
        android:textSize="75dp"
        android:layout_marginRight="20dp"
        android:textColor="#ffffff"
        android:text="0"/>

    <TextView
        android:id="@+id/old_numbers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"
        android:gravity="right"
        android:textSize="25dp"
        android:layout_marginRight="20dp"
        android:textColor="#ffffff"
        android:text="0"/>

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

        <Button
            android:id="@+id/delete_all"
            android:height="@dimen/_80sdp"
            android:textSize="35sp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="clearAllNumbers"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="AC"/>

        <Button
            android:id="@+id/plus_minus"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="+/-"/>

        <Button
            android:id="@+id/percent"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="%"/>

        <Button
            android:id="@+id/minus"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="calcButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="-"/>

    </LinearLayout>

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

        <Button
            android:id="@+id/one"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="1"/>

        <Button
            android:id="@+id/two"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="2"/>

        <Button
            android:id="@+id/three"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="3"/>

        <Button
            android:id="@+id/plus"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="calcButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="+"/>

    </LinearLayout>

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

        <Button
            android:id="@+id/four"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="4"/>

        <Button
            android:id="@+id/five"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="5"/>

        <Button
            android:id="@+id/six"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="6"/>

        <Button
            android:id="@+id/multiplication"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="calcButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="*"/>

    </LinearLayout>

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

        <Button
            android:id="@+id/seven"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="7"/>

        <Button
            android:id="@+id/eighte"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="8"/>

        <Button
            android:id="@+id/nine"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="9"/>

        <Button
            android:id="@+id/division"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="calcButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="/"/>

    </LinearLayout>

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

        <Button
            android:id="@+id/zero"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight=".67"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="numberButtonClick"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="0"/>

        <Button
            android:id="@+id/dot"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="."/>

        <Button
            android:id="@+id/equals"
            android:height="@dimen/_80sdp"
            android:textSize="35dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="equalButtonDisplay"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff"
            android:text="="/>

        </LinearLayout>
    </LinearLayout>
&#13;
&#13;
&#13;

我有问题。我的计算器无法适应不同的设备。你可以在这两张照片上看到这个问题。我需要做什么?我希望他们将在al device

中全屏(按钮)

big device

small device

2 个答案:

答案 0 :(得分:0)

我发现这个库对可伸缩屏幕非常有用......

https://github.com/intuit/sdp

答案 1 :(得分:0)

@Ilya Fedoseev,添加您的布局(.xml文件)代码,人们可以通过该代码建议您进行透视修改。

查看更新后的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:orientation="vertical"
    tools:context="com.example.ilafedoseev.calculatorkotlin.MainActivity">

    <TextView
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="0"
        android:textColor="#ffffff"
        android:textSize="75dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="0dp" />

    <TextView
        android:id="@+id/old_numbers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="0"
        android:textColor="#ffffff"
        android:textSize="25dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="0dp" />

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="5">

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

            <Button
                android:id="@+id/delete_all"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="clearAllNumbers"
                android:text="AC"
                android:textColor="#ffffff"
                android:textSize="35sp" />

            <Button
                android:id="@+id/plus_minus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:text="+/-"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/percent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:text="%"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/minus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="calcButtonClick"
                android:text="-"
                android:textColor="#ffffff"
                android:textSize="35dp" />

        </LinearLayout>

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

            <Button
                android:id="@+id/one"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="1"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/two"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="2"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/three"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="3"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/plus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="calcButtonClick"
                android:text="+"
                android:textColor="#ffffff"
                android:textSize="35dp" />

        </LinearLayout>

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

            <Button
                android:id="@+id/four"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="4"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/five"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="5"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/six"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="6"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/multiplication"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="calcButtonClick"
                android:text="*"
                android:textColor="#ffffff"
                android:textSize="35dp" />

        </LinearLayout>

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

            <Button
                android:id="@+id/seven"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="7"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/eighte"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="8"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/nine"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="9"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/division"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="calcButtonClick"
                android:text="/"
                android:textColor="#ffffff"
                android:textSize="35dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <Button
                android:id="@+id/zero"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".67"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="numberButtonClick"
                android:text="0"
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/dot"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:text="."
                android:textColor="#ffffff"
                android:textSize="35dp" />

            <Button
                android:id="@+id/equals"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="@dimen/_80sdp"
                android:background="@drawable/button_selector"
                android:onClick="equalButtonDisplay"
                android:text="="
                android:textColor="#ffffff"
                android:textSize="35dp" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>