我在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
特别是我有三个问题:
- 为什么是
desiredHeightDP: 28
.我只有2个dimens.xml文件.在一个小屏幕上,它应该是10dp,在另一个屏幕上,它应该是700dp.这28个是从哪里来的. - Pixcel 3在横向模式下的宽度为786 dp,根据Android Studio虚拟设备管理器中的属性,高度为393 dp.为什么宽度的输出与737dp略有不同,而高度的输出与392 dp略有不同?
- 即使宽度减小到737dp,应用程序也应该从"Values-sw600dp"文件夹中的dimens.xml文件中 Select 值,因为最小宽度超过600dp.但它没有做到这一点.
有人能帮我回答这三个问题吗?总体目标是根据屏幕大小更改布局中某些元素的大小.我被建议使用为不同的最小宽度定义不同文件夹的方法,如"Values-sw600dp