Android 屏幕尺寸无效?
我知道并应用这个表单来获得屏幕的尺寸,但是有些东西失败了,因为显然它不是在所有分辨率下都有效。例如,如果我为小分辨率定义了一些按钮的位置,当我以更高的分辨率运行应用程序时,这些按钮会向下滑动。在下图中,我给出了按钮在Emulator 2.7 QVGA API 25(小型)和Nexus 7 API 25(xlarge)中的显示示例。我是否可以断定Android Studio失败了?请帮我看看我的问题,因为我之前已经搜索并列出了它,但我还没有收到任何正确的答案。 这是我的xml:Android 屏幕尺寸无效?,android,android-studio,Android,Android Studio,我知道并应用这个表单来获得屏幕的尺寸,但是有些东西失败了,因为显然它不是在所有分辨率下都有效。例如,如果我为小分辨率定义了一些按钮的位置,当我以更高的分辨率运行应用程序时,这些按钮会向下滑动。在下图中,我给出了按钮在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="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>
项目的Minsdk版本是什么?Minsdk版本17编译了Minsdk版本28;首先,你的应用程序不会在2.7英寸的真正Android设备上运行,因为没有。其次,如果你想实现这个目标,你可以使用Gridlayout。由于有许多不同屏幕配置的Android设备,因此很难在每台设备上实现相同的UI。好的,该应用程序不会在2.7英寸上运行,但是Galaxy Nexus API 25的按钮也正好出现在四个正方形上,那又如何呢?我已经看到并证明了这一点,但问题仍然存在。对不起,我忘了清理Project。我会做一些改变,我会告诉你。我有一个问题:当点击每个按钮时,我希望它变成红色。但是,下面的两个按钮仍然需要在错误的位置单击。我想看看如何解决这个问题。我想我已经解决了你的问题。请接受答案。如果你仍然有问题,你可以问一个新问题,因为维度问题已经解决了。
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>