【问题标题】:How to change button size according to screen's device?如何根据屏幕设备更改按钮大小?
【发布时间】:2016-09-15 17:45:07
【问题描述】:

我有一个尺寸为width=216height=110 的按钮,设备屏幕分辨率为1080 x 1920。当我更改为其他设备(较小的分辨率)时,按钮太大了。因此,我需要编写一个函数来根据屏幕的设备分辨率动态更改按钮大小。首先,我将检查目标设备分辨率,然后根据当前设备进行更改。但是,按钮分辨率不正确。我该如何解决?这是我的代码

我的代码在 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);

【问题讨论】:

  • 你读过Supporting Multiple Layouts吗?如果没有,我建议你继续看看。
  • @Abbas:它来自 xml 和 drawable 文件夹。我想用编程改变我的按钮
  • 那么你将不得不做很多价值很少的工作。我建议你通过布局来做到这一点。检查@slackwars 答案,这是实现您想要的最佳方式。但是,该解决方案仅在您具有相似布局时才有效。对于不同尺寸的不同布局,您必须添加多个布局。
  • 因为按钮的大小必须根据参考仔细选择一次。因此,我需要使用代码/编程来做到这一点,而不是 xml 文件
  • 1) 你为什么写例如HEIGHT = metrics.widthPixels; ? 2) 你能解释/添加一张你想要什么与你目前得到什么的图片吗? 3)如果我们让其他一切正常工作:由于 Button 不是方形的,这可能看起来不错,只是在横向上变得一团糟。

标签: android android-layout android-widget android-linearlayout


【解决方案1】:

在这种情况下,我会为不同的屏幕尺寸要求创建一个特定的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"
          />

【讨论】:

  • 您如何计算按钮尺寸以获得比参考分辨率更小或更大的分辨率(我的案例是1080 x 1920)?
  • 转到:developer.android.com/guide/practices/screens_support.html 并向下滚动到 Using new size qualifiers 这将简要介绍如何格式化项目文件夹以支持多种屏幕分辨率。
【解决方案2】:

你必须使用 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>

【讨论】:

  • 地狱0。我想用编程来改变按钮的大小,而不是 xml
【解决方案3】:

添加文件夹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

【讨论】:

  • 对不起。我当前的分辨率是 1080。我想使用代码计算比参考分辨率更小或更大的按钮分辨率
  • 所以添加其他值,在答案中设置名称并创建维度。
猜你喜欢
  • 2016-04-22
  • 2020-08-14
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
相关资源
最近更新 更多