根据屏幕分辨率以编程方式创建视图
Creating a View programmatically according to screen resolution
我创建了一个实现 ListAdapter 的 BaseAdapter。这个 BaseAdapter 读取一个 JSON 文件并填充我的 ListView 以扩展自定义布局。
在这个 JSON 文件中,我有一个布尔值(active = true 或 active = false)。如果 active 为 true,我会膨胀自定义布局,一切正常,但如果 active 为 false,我需要在此自定义布局中创建一个新视图。这是我的代码:
if (!active) {
JSONObject notifications = jsonData.getJSONObject("notifications");
String message = notifications.getString("message");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.height = 70;
TextView warning = new TextView(Controller.getContext());
warning.setText(message);
warning.setTextColor(Color.WHITE);
warning.setBackgroundColor(Color.RED);
warning.setGravity(Gravity.CENTER);
warning.setPadding(10, 0, 10, 0);
warning.setLayoutParams(params);
// contentFrame is a RelativeLayout where the view is created;
viewHolder.contentFrame.getLayoutParams().height = 320;
viewHolder.contentFrame.addView(warning);
}
我的问题是:
params.height = 70;
viewHolder.contentFrame.getLayoutParams().height = 320;
我是说我的 TextView 高了 70 像素,我的 RelativeLayout 现在高了 320 像素。
它在我的 phone 上看起来不错,但如果我在 phone 上测试其他分辨率,它看起来会不一样吧?
有没有办法创建一个View,并根据屏幕分辨率定义高度或宽度?也许创建屏幕分辨率大小的 10% 的视图?这是一个好的做法吗?对于这样的事情,最好的方法是什么?
不使用硬编码像素值,而是使用 DP。 DP 考虑了设备密度,因此无论应用程序在哪个设备上运行,视图看起来都几乎相同。
有关其他信息,请参阅 this。
因此,在您的 dimens.xml 中定义它:
<resources>
<dimen name="my_width">320dp</dimen>
</resources>
然后这样读:
int width = getResources().getDimensionPixelSize(R.dimen.my_width);
您可以在 XML like any other resource 中定义维度,并让 Android 根据当前设备配置自动选择这些资源。
所以你有两个定义,一个用于 "normal" 情况,另一个用于 "bigger screen":
src/main/res/values/dimens.xml
src/main/res/values-sw600dp/dimens.xml
每个资源文件都可以在 dp.h 中定义相同的维度。 Android 会选择合适的。如果您需要以编程方式读取这些值作为原始像素大小,您可以使用 Resources.getDimensionPixelSize().
我建议不要尝试根据屏幕大小的百分比来定义列表项。
您必须根据屏幕密度缩放尺寸值:
float density = getResources().getDisplayMetrics().density;
这意味着您必须将所有尺寸值乘以 density
:
params.height = (int)(70 * density);
viewHolder.contentFrame.getLayoutParams().height = (int)(320 * density);
与填充值相同:
warning.setPadding((int)(10 * density), 0, (int)(10 * density), 0);
我创建了一个实现 ListAdapter 的 BaseAdapter。这个 BaseAdapter 读取一个 JSON 文件并填充我的 ListView 以扩展自定义布局。
在这个 JSON 文件中,我有一个布尔值(active = true 或 active = false)。如果 active 为 true,我会膨胀自定义布局,一切正常,但如果 active 为 false,我需要在此自定义布局中创建一个新视图。这是我的代码:
if (!active) {
JSONObject notifications = jsonData.getJSONObject("notifications");
String message = notifications.getString("message");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.height = 70;
TextView warning = new TextView(Controller.getContext());
warning.setText(message);
warning.setTextColor(Color.WHITE);
warning.setBackgroundColor(Color.RED);
warning.setGravity(Gravity.CENTER);
warning.setPadding(10, 0, 10, 0);
warning.setLayoutParams(params);
// contentFrame is a RelativeLayout where the view is created;
viewHolder.contentFrame.getLayoutParams().height = 320;
viewHolder.contentFrame.addView(warning);
}
我的问题是:
params.height = 70;
viewHolder.contentFrame.getLayoutParams().height = 320;
我是说我的 TextView 高了 70 像素,我的 RelativeLayout 现在高了 320 像素。
它在我的 phone 上看起来不错,但如果我在 phone 上测试其他分辨率,它看起来会不一样吧?
有没有办法创建一个View,并根据屏幕分辨率定义高度或宽度?也许创建屏幕分辨率大小的 10% 的视图?这是一个好的做法吗?对于这样的事情,最好的方法是什么?
不使用硬编码像素值,而是使用 DP。 DP 考虑了设备密度,因此无论应用程序在哪个设备上运行,视图看起来都几乎相同。
有关其他信息,请参阅 this。
因此,在您的 dimens.xml 中定义它:
<resources>
<dimen name="my_width">320dp</dimen>
</resources>
然后这样读:
int width = getResources().getDimensionPixelSize(R.dimen.my_width);
您可以在 XML like any other resource 中定义维度,并让 Android 根据当前设备配置自动选择这些资源。
所以你有两个定义,一个用于 "normal" 情况,另一个用于 "bigger screen":
src/main/res/values/dimens.xml
src/main/res/values-sw600dp/dimens.xml
每个资源文件都可以在 dp.h 中定义相同的维度。 Android 会选择合适的。如果您需要以编程方式读取这些值作为原始像素大小,您可以使用 Resources.getDimensionPixelSize().
我建议不要尝试根据屏幕大小的百分比来定义列表项。
您必须根据屏幕密度缩放尺寸值:
float density = getResources().getDisplayMetrics().density;
这意味着您必须将所有尺寸值乘以 density
:
params.height = (int)(70 * density);
viewHolder.contentFrame.getLayoutParams().height = (int)(320 * density);
与填充值相同:
warning.setPadding((int)(10 * density), 0, (int)(10 * density), 0);