J'essaye de faire un AlertDialog qui adapte la taille au contenu. Le contenu textuel peut être plus grand que le dialogue, il doit donc être capable de défiler. Si j'utilise un RelativeLayout, tout est rendu correctement, peu importe la quantité d'informations qu'il contient, mais lorsqu'il n'y a pas assez de texte pour remplir TextView, cela laisse beaucoup d'espace supplémentaire. Voici le code:RelativeLayout dans le dialogue a plus d'espace
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
android:paddingBottom="2dip"
>
<CheckBox
android:id="@+id/saleListingCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save sale"
android:textColor="#fff"
android:layout_alignParentBottom="true"
>
</CheckBox>
<ScrollView
android:id="@+id/saleListingLinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/saleListingCheckbox"
android:layout_alignParentTop="true"
>
<TextView
android:id="@+id/saleListingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="7pt"
android:paddingBottom="4dip"
/>
</ScrollView>
</RelativeLayout>
code java:
@Override
protected boolean onTap(int index) {
if (overlays.isEmpty()) {
return false;
}
final SaleOverlayPushPin saleItem = overlays.get(index);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
LayoutInflater inflater = context.getLayoutInflater();
dialogView = inflater.inflate(R.layout.sale_listing, null);
textView = (TextView) dialogView.findViewById(R.id.saleListingTextView);
checkbox = (CheckBox) dialogView.findViewById(R.id.saleListingCheckbox);
checkbox.setOnCheckedChangeListener(this);
alertDialog.setView(dialogView);
alertDialog.setTitle(saleItem.getTitle());
textView.setText(saleItem.getSnippet());
checkbox.setTag(saleItem);
checkbox.setChecked(saleItem.isSelected());
alertDialog.show();
return true;
}
Et voici à quoi il ressemble avec peu de données et beaucoup de données:
Je été en mesure de le faire fonctionner en utilisant un LinearLayout, mais j'ai un problème différent où si le contenu du texte est plus grand que le dialogue, il coupe la case à cocher. Voici le code et les captures d'écran:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
android:paddingBottom="2dip"
android:orientation="vertical"
>
<ScrollView
android:id="@+id/saleListingLinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="top"
>
<TextView
android:id="@+id/saleListingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="7pt"
android:paddingBottom="4dip"
/>
</ScrollView>
<CheckBox
android:id="@+id/saleListingCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save sale"
android:textColor="#fff"
android:layout_weight="1"
android:layout_gravity="bottom"
>
</CheckBox>
</LinearLayout>
Je préférerais que le comportement « peu de données » fonctionne comme le LinearLayout et le « grand nombre de données » fonctionnent comme le RelativeLayout. Est-ce possible?
MISE À JOUR: la solution de bart pour le LinearLayout fonctionne parfaitement. Retrait du poids de la disposition de la CheckBox était la clé.
Toutefois, RelativeLayout ne fonctionne toujours pas comme prévu. Cela rend le CheckBox invivable si les données dans le TextView sont assez grandes. J'aimerais toujours savoir la solution pour RelativeLayout si possible. Voir ci-dessous:
Merci, mais le RelativeLayout que vous proposez rend le CheckBox invisibles si le TextView a suffisamment de données pour faire une barre de défilement. Le LinearLayout fonctionne parfaitement bien. Merci! – Nemi