簡體   English   中英

如何根據屏幕設備更改按鈕大小?

[英]How to change button size according to screen's device?

我在設備屏幕分辨率中有一個width=216height=110的按鈕,屏幕分辨率為1080 x1920。當我切換到其他設備(分辨率較低)時,該按鈕太大。 因此,我需要編寫一個函數來根據屏幕的設備分辨率動態更改按鈕的大小。 首先,我將檢查目標設備的分辨率,然后根據當前設備的上方進行更改。 但是,按鈕分辨率不正確。 我該如何解決? 這是我的代碼

我在1080x1920中的代碼

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        >
        <Button
            android:id="@+id/btn"
            android:layout_width="216dp"
            android:layout_height="110dp"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
          />
       </LinearLayout>

更改為動態方式:

    public int WIDTH ;
    public int HEIGHT ;
    //Get screen resolution
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    HEIGHT = metrics.heightPixels;
    WIDTH= metrics.widthPixels;           

    int height_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
    int weight_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 216, getResources().getDisplayMetrics());
    int width_linearlayout = Math.round((float)weight_px/1080*WIDTH);
    int height_linearlayout = Math.round((float)height_px/1920*HEIGHT);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width_linearlayout,height_linearlayout);
    btn.setLayoutParams(layoutParams);

在這種情況下,我將為不同的屏幕尺寸要求創建一個特定的dimens.xml。

Android Studio dimens.xml視圖

其中每個參數都包含特定於帶有適合這些尺寸的屏幕的設備的參數。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- Default screen margins, per the Android Design guidelines. -->
  <dimen name="nav_header_height">160dp</dimen>
  <!-- Default screen margins, per the Android Design guidelines. -->
  <dimen name="activity_horizontal_margin">16dp</dimen>
  <dimen name="activity_vertical_margin">16dp</dimen>
  <dimen name="button_size_height">32dp</dimen>
  <dimen name="button_size_width">70dp</dimen>
</resources>

無需額外的編碼,只需維護包含您的按鈕參數的多個xml文件。

每個維度都將這樣引用:

<Button
            android:id="@+id/btn"
            android:layout_width="@dimen/button_size_width"
            android:layout_height="@dimen/button_size_height"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
          />

您必須使用layout_weight來實現所需的功能,並且通過將每行按鈕包裝在LinearLayout中來使用,而所有LinearLayouts和TextView都將包裝在一個大的LinearLayout中,而不是如下所示的RelativeLayout中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000"
    android:orientation="vertical"
    android:weightSum="6" >

<TextView
    android:id="@+id/disp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:inputType="none"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/white"
    android:textSize="32sp"
    android:textStyle="normal" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <Button
        android:id="@+id/clear"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:background="@drawable/clear_btn" />

    <!-- then the three remaining buttons -->

    <LinearLayout />

    <!-- then the four remaining rows -->


    <!-- in the last row the "0" button will have layout_weight="2" NOT "1" -->
</LinearLayout>

添加文件夾values-sw540dp它是用於1080p屏幕的 在此文件夾中添加dimens.xml

  <dimen name="btn_height">100dp</dimen>
  <dimen name="btn_width">65dp</dimen>

編輯您的xml

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        >
        <Button
            android:id="@+id/btn"
            android:layout_width="@dimen/btn_width"
            android:layout_height="@dimen/btn_height"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
          />
       </LinearLayout>

其他決議

    values-sw720dp          10.1” tablet 1280x800 mdpi

values-sw600dp          7.0”  tablet 1024x600 mdpi

values-sw480dp          5.4”  480x854 mdpi 
values-sw480dp          5.1”  480x800 mdpi 

values-xhdpi            4.7”   1280x720 xhdpi 
values-xhdpi            4.65”  720x1280 xhdpi 

values-hdpi             4.0” 480x800 hdpi
values-hdpi             3.7” 480x854 hdpi

values-mdpi             3.2” 320x480 mdpi

values-ldpi             3.4” 240x432 ldpi
values-ldpi             3.3” 240x400 ldpi
values-ldpi             2.7” 240x320 ldpi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM