我在Android中有一个简单的XML布局文件,只有一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

下面是片段的对应Java文件:

public class FR_Test extends Fragment {


    public FR_Test() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }
    private FragmentTestBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentTestBinding.inflate(inflater, container, false);


        int desiredHeightDP = getResources().getDimensionPixelSize(R.dimen.height_line_graph);
        Log.e("DR_Stats", "desiredHeightDP: " + desiredHeightDP);
        float density = getResources().getDisplayMetrics().density;
        Log.e("DR_Stats", "density: " + density);
        int desiredHeightPixels = (int) (desiredHeightDP * density);
        Log.e("DR_Stats", "desiredHeightPixels: " + desiredHeightPixels);

        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int screenWidthDp = (int) (displayMetrics.widthPixels / displayMetrics.density);
        int screenHeightDp = (int) (displayMetrics.heightPixels / displayMetrics.density);

        Log.e("DR_Stats", "screenWidthDp: " + screenWidthDp);
        Log.e("DR_Stats", "screenHeightDp: " + screenHeightDp);

        binding.button.setHeight(desiredHeightPixels);



        return binding.getRoot();
    }
}

我想根据使用的显示器大小来设置按钮的高度.为此,我在标准值文件夹旁边定义了包含维度文件的值文件夹"Values-sw600dp

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="height_line_graph">700dp</dimen>

</resources>"

在默认值文件夹中,dimension(dimens.xml)文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="height_line_graph">10dp</dimen>
</resources>

当在Android Studio中使用Pixel 3作为模拟器时,它的dp分辨率为393 x 786,在横向模式下,我得到了一个我根本不理解的输出:

2023-12-03 11:51:01.102  7448-7448  DR_Stats                com.example.drinkserver              E  desiredHeightDP: 28
2023-12-03 11:51:01.102  7448-7448  DR_Stats                com.example.drinkserver              E  density: 2.75
2023-12-03 11:51:01.102  7448-7448  DR_Stats                com.example.drinkserver              E  desiredHeightPixels: 77
2023-12-03 11:51:01.102  7448-7448  DR_Stats                com.example.drinkserver              E  screenWidthDp: 737
2023-12-03 11:51:01.102  7448-7448  DR_Stats                com.example.drinkserver              E  screenHeightDp: 392 

特别是我有三个问题:

  1. 为什么是desiredHeightDP: 28.我只有2个dimens.xml文件.在一个小屏幕上,它应该是10dp,在另一个屏幕上,它应该是700dp.这28个是从哪里来的.
  2. Pixcel 3在横向模式下的宽度为786 dp,根据Android Studio虚拟设备管理器中的属性,高度为393 dp.为什么宽度的输出与737dp略有不同,而高度的输出与392 dp略有不同?
  3. 即使宽度减小到737dp,应用程序也应该从"Values-sw600dp"文件夹中的dimens.xml文件中 Select 值,因为最小宽度超过600dp.但它没有做到这一点.

有人能帮我回答这三个问题吗?总体目标是根据屏幕大小更改布局中某些元素的大小.我被建议使用为不同的最小宽度定义不同文件夹的方法,如"Values-sw600dp

推荐答案

忽略问题2(因为我不经常使用模拟器,而且我现在很少关心screen大小与window大小……).

为什么是想要的高度DP:28.我只有2个dimens.xml文件.在一个小屏幕上,它应该是10dp,在另一个屏幕上,它应该是700dp.这28个是从哪里来的.

你要打的是getDimensionPixelSize().它返回以像素为单位的值,而不是dp.你的密度是2.75,所以10dp大约是28个像素.

即使稍微减少了737 dp的宽度,应用程序也应该从"values-sw 600 dp"文件夹中的dimens.xml文件中 Select 值,因为最小宽度超过600 dp.但它并没有这样做.

最小的宽度是最小的X/Y尺寸.对于纵向的手机,最小宽度为当前宽度.对于一部风景中的手机来说,最小的宽度是目前的height.因此,最小宽度与方向无关.而且,在您的情况下,最小宽度是393dp.

在这款应用程序中,一切都将处于风景模式.所以我不确定我应该用values-sw600dp还是values-w600dp

现成的,我会用values-w600dp,但同样,这取决于具体的设计.

Java相关问答推荐

了解Android Studio中的调试器输出

Jlink选项&-strie-ative-Commands";的作用是什么?

Java Swing:初始化身份验证类后未检测到ATM_Interface键事件

试着做一个2x2的魔方求解算法,我如何找到解路径(DFS)?

如何修复PDF重建过程中的文本定位

为什么Spring Boot项目无法为基于MySQL的CRUD应用程序找到从JPARepository接口扩展的ProductRepository?

在Eclipse中数组的可空性

为什么Collectors.toList()不能保证易变性

舰队运行配置Maven版本

Spring Framework6.1中引入的新RestClient是否有适合于测试的变体,就像RestTemplate和TestRestTemplate一样?

Spring Validator批注不起作用

嘲笑黄瓜中的对象

如何在Maven Central上部署?

通过/失败的参数化junit测试方法执行数

OAuth:登录后无法查看Google邮箱地址

字符串的Gzip压缩在java11和java17中给出了不同的结果

如何在特定关键字后提取与模式匹配的多个值?

java.lang.NoSuchMethodError:';org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4

java.util.LinkedList()是如何成为MutableList的实例的?

为什么 log4j 过滤器在appender中不起作用