我的目标是点击每颗星时出现一个红色圆圈。一切都以完全分辨率运行。问题在于,当以其他分辨率运行应用程序时,按钮以较小的分辨率向下滑动,甚至在小分辨率中使圆形不规则,如两个示例图像中所示。我知道问题的来源,但我不知道如何解决它:它是一个高度问题。宽度总是受到尊重,但不是高度。我假装通过在定位按钮时改变高度和宽度来解决它,但是比例也没有保持不同的分辨率。我已经尝试解决这个问题一段时间,但我不能。我很感激你的帮助。这是代码:
public class MainActivity extends AppCompatActivity {
View pulsado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Width and height of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
int height = metrics.heightPixels;
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1); // id del XML
for (int i = 1; i < 8; i++) {
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Size of BUTTONs
rel_btn.width = Math.round((float) (3.6*width/100)); rel_btn.height = Math.round((float) (5.8*height/100));
// Position of BUTTONs
switch(i) {
case 1: rel_btn.leftMargin = Math.round((float) (9.9*width/100)); rel_btn.topMargin = Math.round((float) (24.75*height/100)); break;
case 2: rel_btn.leftMargin = Math.round((float) (25.45*width/100)); rel_btn.topMargin = Math.round((float) (22.0*height/100)); break;
case 3: rel_btn.leftMargin = Math.round((float) (38.75*width/100)); rel_btn.topMargin = Math.round((float) (31.7*height/100)); break;
case 4: rel_btn.leftMargin = Math.round((float) (53.2*width/100)); rel_btn.topMargin = Math.round((float) (44.4*height/100)); break;
case 5: rel_btn.leftMargin = Math.round((float) (79.5*width/100)); rel_btn.topMargin = Math.round((float) (49.2*height/100)); break;
case 6: rel_btn.leftMargin = Math.round((float) (57.8*width/100)); rel_btn.topMargin = Math.round((float) (66.2*height/100)); break;
case 7: rel_btn.leftMargin = Math.round((float) (74.3*width/100)); rel_btn.topMargin = Math.round((float) (69.45*height/100)); break;
}
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0+1); // writes the ID
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.TRANSPARENT);
}
//Width of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
Button button = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.OVAL);
drawable.setStroke(Math.round((float) (0.6*width/100)), Color.RED);
drawable.setColor(Color.TRANSPARENT);
button.setBackgroundDrawable(drawable);
pulsado = view;
}
};