2016-11-22 1 views
0

J'ai un listview dans ma boîte de dialogue et ensuite je dois utiliser la disposition d'en-tête et la disposition du pied de page. la façon dont je le fais, alignant ma disposition d'en-tête vers le haut et la disposition de bas de page vers le bas. mais il a un problème. jetez un oeil à l'image ci-dessous .. Dialog with the list inside with header and footerListview dans la boîte de dialogue avec l'en-tête et le pied de page

Mais en faisant que le dialogue se allonge taille qui ne devrait pas

Je suis dans cette situation et TRAP. Eh bien, si j'aligne mon pied de page à la fin de la liste, le pied de page sort de l'écran. ce qui n'est pas prévu non plus.

Ce que je veux:

  • Je veux que mon listview doit être visible dans le centre de dialogue avec la mise en page d'en-tête d'aligner le dessus et la mise en page de pied de page alignement sur la bootm de la boîte de dialogue borderline et il ne devrait pas prendre toute la longueur de l'écran.
  • N'y a-t-il pas moyen d'aligner le pied de page en bas de la boîte de dialogue et de maintenir sa hauteur (Note: Je ne veux pas donner à la boîte de dialogue la hauteur codée

Aidez-moi, je suis coincé dans la situation. Si nous regardons la boîte de dialogue google qui vient lors de l'installation de l'application, elle a un court dialogue avec la liste au centre de celle-ci et les boutons sont alignés en bas de la boîte de dialogue. Comment ils le font?

Répondre

0

Vous pouvez définir la mise en page à votre Dialog comme ci-dessous manière

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView android:id="@+id/header_txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Header Text" 
     android:gravity="center|top"/> 
    <ListView android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:gravity="center_vertical/> 
    <TextView android:id="@+id/footer_txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Footer Text" 
     android:gravity="center|bottom"/> 
</LinearLayout> 

Il établira Header View to Top, ListView to Center et Footer View to Bottom

+0

wouldnt-il pas prendre le pied de page sur l'écran si le listview est grand? –

+0

@AbdulSalamAli si vos données sont volumineuses, alors mieux vaut donner la valeur 'height' à' ListView' et il n'y a pas d'autre option si vous n'aimez pas cette façon, alors vous avez changé la structure. – Ironman

+0

oui Je viens de le vérifier et le pied de page sort de l'écran si la vue de la liste est trop grande –

0

Vous pouvez essayer celui-ci, .. créer un xml et copiez-collez ce code et définir votre xml dialogue

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <LinearLayout 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:layout_marginTop="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000" 
     android:gravity="center" 
     android:orientation="vertical" 
     > 


     <RelativeLayout 
      android:id="@+id/table_rl" 
      android:padding="10dp" 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:layout_height="0dp" 
      android:orientation="vertical"> 

     <TextView 
      android:id="@+id/your_items_tv" 
      android:textColor="@color/text_black" 
      customFontPath="lato_black.ttf" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:layout_marginTop="20dp" 
      android:text="HEADING" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

      <LinearLayout 
       android:layout_below="@+id/your_items_tv" 
       android:paddingBottom="5dp" 
       android:layout_marginTop="10dp" 
       android:paddingTop="5dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

       <TextView 
        android:textColor="@color/text_black" 
        customFontPath="lato_bold.ttf" 
        android:layout_gravity="center" 
        android:gravity="left" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:layout_height="wrap_content" 
        android:text="TOP BAR"/> 

      </LinearLayout> 
      <ListView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"></LinearLayout> 
     </RelativeLayout> 
     <LinearLayout 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_below="@+id/table_rl" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ff00ff"> 

      <TextView 
       android:padding="5dp" 
       android:textSize="18dp" 
       android:textColor="@color/text_black" 
       customFontPath="lato_bold.ttf" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="Bottom Bar "/> 

     </LinearLayout> 


    </LinearLayout> 
</LinearLayout> 

1

est ici la solution dynamique pour votre problème .. dialogue Exemple Nom du fichier xml activity_list_test.xml ..

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" 
android:background="@color/page_bg" 
android:orientation="vertical"> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Header Button" /> 

<ListView 
    android:id="@+id/lvCommon" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Bottom Button" /></LinearLayout>` 

est ici l'appel d'un fragment/activité où vous voulez ..

private void showAlert() { 

    AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); 
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_list_test, null); 
    dialog.setView(view); 
    dialog.setTitle(null); 
    dialog.setMessage(null); 

    ArrayList<String> mList = new ArrayList<>(); 
    for (int i = 0; i < 15; i++) { 
     mList.add("ABC"); 
    } 


    ListView lvCommon = (ListView) view.findViewById(R.id.lvCommon); 

    int deviceHeight = getScreenSize(getActivity()).y; 
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, deviceHeight/3,1f); 
    lvCommon.setLayoutParams(layoutParams); 
    lvCommon.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mList)); 
    dialog.setCancelable(false); 
    dialog.show(); 
} 

Il hauteur de l'appareil sera mesure automatique et selon elle la taille de la boîte de dialogue sera "deviceheight/3" ..so elle sera maintenue dans tous les appareils ..

Maintenant, vous devez la méthode getScreenSize ..So est ici, il est ::

public static Point getScreenSize(Activity act) { 
    int screenWidth = 0; 
    int screenHeight = 0; 

    final DisplayMetrics metrics = new DisplayMetrics(); 
    act.getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    screenWidth = metrics.widthPixels; 
    screenHeight = metrics.heightPixels; 

    return new Point(screenWidth, screenHeight); 

} 
+0

promettant ,, je vais essayer –