Ich möchte Schaltflächen dynamisch nach verschiedenen Bildschirmgrößen hinzufügen. Wie kann ich es erreichen?7 Tasten dynamisch hinzufügen, um alle Bildschirmgrößen auf seine Breite und Höhe in Android anzupassen
Das habe ich ausprobiert. Aber wenn ich die erste Taste von WRAP_CONTENT auf statische Höhe, Breite keiner der Taste ändern wird auf dem Bildschirm angezeigt
public void setButtons(){
// button one params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(380,433,10,20);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params1.setMargins(229,-45,50,20);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params2.setMargins(239,60,10,20);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params3.setMargins(422,-45,10,20);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params4.setMargins(592,-455,10,20);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params5.setMargins(592,65,10,20);
// LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(76,76);
// params6.setMargins(592,-275,10,20);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params);
// button1.setText("like");
button1.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
// button1.setBackgroundColor(Color.TRANSPARENT);
Button button2 = new Button(getApplicationContext());
button2.setLayoutParams(params1);
// button2.setText("share");
button2.setBackgroundDrawable(getResources().getDrawable(R.drawable.share));
// button2.setBackgroundColor(Color.TRANSPARENT);
Button button3 = new Button(getApplicationContext());
button3.setLayoutParams(params2);
// button3.setText("mute");
button3.setBackgroundDrawable(getResources().getDrawable(R.drawable.mute));
// button3.setBackgroundColor(Color.TRANSPARENT);
Button button4 = new Button(getApplicationContext());
button4.setLayoutParams(params3);
button4.setBackgroundDrawable(getResources().getDrawable(R.drawable.download));
Button button5 = new Button(getApplicationContext());
button5.setLayoutParams(params4);
button5.setBackgroundColor(Color.TRANSPARENT);
Button button6 = new Button(getApplicationContext());
button6.setLayoutParams(params5);
button6.setBackgroundColor(Color.TRANSPARENT);
layout.addView(button1);
layout.addView(button2);
layout.addView(button3);
layout.addView(button4);
layout.addView(button5);
layout.addView(button6);
buttonLayout.addView(layout);
}
originalWidth = 1100;
originalHeight = 2050;
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
int getMyNewX(int originalX)
{
//originalX : x for line segment
return width/originalWidth * originalX;
}
int getMyNewY(int originalY)
{
//originalY : y for line segment
return height/originalHeight * originalY;
}
Verwenden Sie Gewicht von linearlayout – Nisarg
wie und wo es zu verwenden? –
Bitte fügen Sie mehr Kontext zu Ihrer Frage hinzu. Warum müssen Sie diese Schaltflächen dynamisch und nicht im XML-Code hinzufügen? –