2016-11-22 76 views
0

我在我的对话框中有一个列表视图,然后我必须使用页眉布局和页脚布局。我这样做的方式,我的标题布局对齐顶部和页脚布局对齐。但它有一个问题。看看下面的图片.. Dialog with the list inside with header and footer在页眉和页脚对话框中的列表视图

但是,这样做的对话框变长,其中预计不会

我在这种情况下traped大小。那么,如果我对齐我的页脚与列表视图结束,那么页脚离开屏幕。这也是预料不到的。

我想要什么:

  • 我想我的列表视图应该是可见的对话与头部布局的中心对齐顶部和页脚布局对齐到对话框中的bootm bottomtom边框,它不应该采取全屏幕。
  • 是不是有一种方法可以将页脚对齐到对话框底部和对话框底部自行保持其高度(注意:我不想给对话框硬编码的高度,因为我希望对话框在所有设备上看起来相同)

请帮我卡在这种情况。如果我们看看安装应用时出现的Google对话框,它会在其中心有一个简短的对话框,并且按钮对齐到对话框的底部。他们如何做到这一点?

回答

0

您可以设置布局,你Dialog像下面这样

<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

+0

母舰一样拿不出页脚出入画面时,如果列表视图是大? –

+0

@AbdulSalamAli如果你的数据很大,那么更好的方法是你必须给“ListView”修正“height”,如果你不喜欢这种方式,没有其他选择,那么你就改变了结构。 – Ironman

+0

是的,我刚刚检查过,如果列表视图太大,页脚跳出屏幕 –

0

你可以试试这个,......创建一个XML和复制粘贴此代码设置你的对话框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 ..所以这里是::

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

承诺,我会尝试 –