android - 屏幕尺寸无效?
问题描述
我知道并应用表格来获取屏幕的尺寸,但是有些失败,因为显然它在所有分辨率下都无效。例如,如果我为小分辨率定义了一些按钮的位置,那么当我以更高分辨率运行应用程序时,这些按钮会向下滑动。在下图中,我给出了按钮如何出现在 Emulator 2.7 QVGA API 25(小)和 Nexus 7 API 25(xlarge)中的示例。我是否可以得出结论说 Android Studio 失败了?请帮我看看我的问题,因为我之前已经搜索并列出了它,但我没有收到任何正确的答案。这是我的xml:
<android.support.constraint.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"
tools:context=".MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:components="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout1">
</RelativeLayout>
</ScrollView>
这是我的java:
public class MainActivity extends AppCompatActivity {
View pulsado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Width and height of screen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
// Buttons are placed
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
for (int i = 1; i < 5; i++) {
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Size of Buttons
rel_btn.width = 4 * width / 100;
rel_btn.height = 4 * width / 100;
// Position of Buttons
switch (i) {
case 1: // top left
rel_btn.leftMargin = 0;
rel_btn.topMargin = 0;
break;
case 2: // top right
rel_btn.leftMargin = 96 * width / 100;
rel_btn.topMargin = 0;
break;
case 3: // lower left
rel_btn.leftMargin = 0;
rel_btn.topMargin = 80 * height / 100;
break;
case 4: // lower right
rel_btn.leftMargin = 96 * width / 100;
rel_btn.topMargin = 80 * height / 100;
break;
}
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.BLUE);
btnTag.setId(0 + i);
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
}
private View.OnClickListener prueba = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pulsado != null) {
Button button1 = (Button) pulsado;
button1.setBackgroundColor(Color.BLUE);
}
Button button2 = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(8, Color.RED);
button2.setBackgroundDrawable(drawable);
pulsado = view;
}
};
}
解决方案
您可以尝试在布局文件本身中设置按钮。
<android.support.constraint.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"
tools:context=".MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_weight="15"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:components="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout1">
<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@color/colorPrimary"/>
</RelativeLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
推荐阅读
- css - Wordpress 的事件管理器有 flex 问题的两列
- sapui5 - 如何在自由式门户网站的外壳标题中添加图像?
- python - CSRF 验证失败。请求中止。登录时
- imagemagick - ImageMagick 雨刮器效果 gif 帧数更少
- javascript - 如何通过 Datatable 发送带有 ajax 请求的 post 数据
- javascript - Array.sort 不会对对象中具有 NaN 值的数组重新排序
- angular - Moment 类型的参数不能分配给 Date 类型的参数
- google-cloud-platform - Google Cloud Dataprep:维度建模
- sql - MS SQL Server - 在连接查询结果集中选择多列的最大 n 个结果组
- angular5 - Angular 谷歌地图如何在谷歌地图上上传的 KML 图层中突出显示多边形: