Bonjour j'ai un écran principal.
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
class="com.google.Android.gms.maps.SupportMapFragment"/>
<ImageButton
Android:id="@+id/imageButton1"
Android:layout_width="55dp"
Android:layout_height="50dp"
Android:layout_alignParentTop="true"
Android:layout_marginTop="10dp"
Android:layout_toLeftOf="@+id/toggleButton1"
Android:background="@Android:drawable/btn_default"
Android:contentDescription="@string/app_name"
Android:scaleType="centerCrop"
Android:src="@drawable/plus_grey" />
<LinearLayout
Android:id="@+id/lay"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentBottom="true"
Android:layout_alignParentLeft="true"
Android:layout_alignParentRight="true"
Android:layout_marginEnd="0dp"
Android:background="#A0FFFFFF"
Android:orientation="horizontal"
Android:visibility="invisible" >
<TextView
Android:id="@+id/locinfo"
Android:layout_width="match_parent"
Android:layout_height="60dp"
Android:textColor="@color/cherniy"
Android:textSize="20sp"
Android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
Et il y a un imageButton
button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bIcon == false) {
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
}
else {
button.setImageResource(R.drawable.plus_grey);
m.remove();
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t.setVisibility(View.INVISIBLE);
linLayout.setVisibility(View.INVISIBLE);
bIcon = false;
}
}
});
Je veux ajouter par programme
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
où w
display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
w = display.getWidth();
Mais quand j'aime ça
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
Mon application se bloque. Pouvez-vous s'il vous plaît dire comment créer par programme avec mes paramètres (liés à la largeur et la hauteur de l'écran de l'appareil)? Je vais prendre des suggestions. Je vous remercie.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( (w*2)/3, LayoutParams.WRAP_CONTENT ); linLayout.setLayoutParams(lp);
Cela provoquera une erreur car la LinearLayout
est l'enfant de la mise en page relative, vous devez donc définir RelativeLayoutParams
pour cela.
Utilisez plutôt ceci
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(w*2)/3,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
ou Au lieu de créer une nouvelle LayoutParams
, vous pouvez réutiliser la LayoutParams
existante de la vue, comme indiqué ci-dessous.
RelativeLayout.LayoutParams lp = linLayout.getLayoutParams();
lp.width = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);
Au lieu de créer un nouveau LayoutParams, vous devez modifier celui existant.
Changez votre code pour:
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = linLayout.getLayoutParams();
final int left_margin = whatever;
final int top_margin = 0;
final int right_margin = whatevermore;
final int bottom_margin = 0;
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
lp.width = (w*2)/3;
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;