2016-11-22 11 views
0

ダイアログボックスにリストビューがあり、ヘッダーレイアウトとフッターレイアウトを使用する必要があります。ヘッダーのレイアウトを一番上に、フッターのレイアウトを一番下に合わせています。それには問題があります。 ...以下の絵を見てみDialog with the list inside with header and footerヘッダーとフッターのダイアログボックスのリストビュー

しかしダイアログは、私はこのような状況でtrapedい

期待されていないサイズが長くなることを行うことによって 。まあ私はリストビューの端と私のフッターを合わせると、フッターは、画面から取得します。これも期待されていない。私が欲しいもの

  • 私は私のリストビューがヘッダーレイアウトアライントップとの対話の中央に表示されている必要がありとフッターのレイアウトは、ダイアログのbootmに合わせることをしたいですボトムの境界線であり、画面の全長を占めるべきではありません。
  • フッターをダイアログの下部に揃える方法はありませんでした。ダイアログでは、その高さを自己維持します(注:ダイアログをすべてのデバイスで同じに見せたいので、ダイアログにハードコードされた高さを与えたくありません。 。)

私は状況に立ち往生してください。アプリケーションをインストールしているときに表示されるGoogleダイアログを見ると、その中央のリストビューとの短いダイアログがあり、ボタンはダイアログの下部に揃っています。彼らはどのようにそれをやっている?

答えて

0

あなたは、以下の方法のように、あなたのDialogにレイアウトを設定することができますが、1つのXMLを作成します..これを試してみて、このコードをコピー&ペーストすることができます

+0

リストビューが大きい場合には、画面の外にフッターを取ることはありませwouldntのは? –

+0

@AbdulSalamAliあなたのデータが大きい場合は、 'ListView'に' height'を修正するより良い方法です。この方法が気に入らなければ、構造を変更する必要はありません。 – Ironman

+0

はいリストビューのサイズが大きすぎると、チェックしたところでフッタが画面外に出る –

0

<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> 

それが設定されますHeader View to TopListView to CenterFooter View to BottomとあなたのダイアログXML設定

<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

ここにあなたの問題のための動的な解決策は... サンプルダイアログ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>` 

ここであなたが望むフラグメント/活動からの呼び出しだ。..

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(); 
} 

デバイスの高さは自動的に測定され、それに応じてダイアログのサイズは"deviceheight/3"になります。すべてのデバイスで維持されます。

今、あなたはここに方法getScreenSize ..Soを必要とするが、それがある::

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

、私は試してみてください –

関連する問題