簡體   English   中英

如何在Android中以編程方式設置列表視圖高度

[英]How to set list view height programatically in android

我正在制作一個應用程序,其中我的鍵盤出現在活動上,並且它隱藏了可能的標題視圖(布局),我已搜索並嘗試隱藏隱藏的窗格狀態,沒有調整清單中的所有屬性,但沒有解決任何問題,然后發現了一個問題在stackover流上發布信息,您可以計算間距然后設置我實現的高度,但是行為仍然相同,這是活動到來時的快照 在此處輸入圖片說明

但是當keyborad彈出時,它隱藏了我的標題視圖,這是正在發生的情況的快照 在此處輸入圖片說明

這是我的清單代碼

 <activity
        android:name="com.dd.sproutchat.ChatActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustNothing">
    </activity>

這是我的活動布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <RelativeLayout
            android:id="@+id/TopLayout"
            android:layout_width="match_parent"
            android:layout_height="55dp">

            <ImageButton
                android:id="@+id/btn_back"
                android:layout_width="20dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:background="@drawable/back_icon_2x"
                android:textColor="#000000"
                android:textSize="22sp" />

            <LinearLayout
                android:id="@+id/Image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/btn_back">

                <Button
                    android:id="@+id/recUserImg"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@drawable/image_circle_shape"
                    android:text=""
                    android:textColor="@color/white"
                    android:visibility="gone" />

                <com.dd.sproutchat.customcontrols.MLRoundedImageView
                    android:id="@+id/userImg"
                    android:layout_width="50dp"
                    android:layout_height="50dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_toLeftOf="@+id/btn_Search"
                android:layout_toRightOf="@+id/Image"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtUserName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="start"
                    android:gravity="left"
                    android:singleLine="true"
                    android:text=""
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black" />

                <TextView
                    android:id="@+id/txtOnlineStatus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:singleLine="true"
                    android:text=""
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@android:color/black" />
            </LinearLayout>

            <ImageButton
                android:id="@+id/btn_Menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/menu_icon_2x"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/btn_Search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/search_icon_2x" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rl_line"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/TopLayout"
            android:background="@color/chat_border"></RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rl_Options"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@+id/Rl_line"
            android:background="@color/chat_options_bg">

            <ImageButton
                android:id="@+id/btn_Home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:background="@drawable/home_btn_active_2x" />

            <ImageButton
                android:id="@+id/btn_SproutesList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:background="@drawable/chat_btn_2x" />

            <ImageButton
                android:id="@+id/btn_Note"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:background="@drawable/note_btn_2x" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rl_line2"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/Rl_Options"
            android:background="@color/chat_border"></RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rl_ChatLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/Rl_MessageLayout"
            android:layout_below="@+id/Rl_line2">

            <!-- android:background="@drawable/chat_bg_2x" -->

            <RelativeLayout
                android:id="@+id/Btn_Chats"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:weightSum="4">

                <RelativeLayout
                    android:id="@+id/count"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone">

                    <ImageButton
                        android:id="@+id/btn_Sortby"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon_sortby_sprout_2x" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_toLeftOf="@+id/btn_AddSprout"
                        android:layout_toRightOf="@+id/btn_Sortby"
                        android:weightSum="2">

                        <Button
                            android:id="@+id/btn_AllSprouts"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="@drawable/btn_blue"
                            android:paddingBottom="3dp"
                            android:paddingLeft="25dp"
                            android:paddingRight="25dp"
                            android:paddingTop="3dp"
                            android:text="@string/AllSprouts"
                            android:textColor="@color/white" />

                        <Button
                            android:id="@+id/btn_AllFavorites"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="30dp"
                            android:layout_weight="1"
                            android:background="@drawable/btn_blank"
                            android:paddingBottom="3dp"
                            android:paddingLeft="15dp"
                            android:paddingRight="15dp"
                            android:paddingTop="3dp"
                            android:text="@string/AllFavorites"
                            android:textColor="@color/grey_start" />
                    </LinearLayout>

                    <ImageButton
                        android:id="@+id/btn_AddSprout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="10dp"
                        android:background="@drawable/icon_add_sprout_2x" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/noteLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:visibility="gone">

                    <EditText

                        android:id="@+id/searchBar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_toLeftOf="@+id/imageButton4" />

                    <ImageButton
                        android:id="@+id/imageButton4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_toLeftOf="@+id/imageButton5"
                        android:background="@drawable/sort" />

                    <ImageButton
                        android:id="@+id/imageButton5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/add" />
                </RelativeLayout>
            </RelativeLayout>

            <ListView
                android:id="@+id/Lv_Chat"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/Btn_Chats"
                android:layout_above="@+id/Rl_MessageLayout"
                android:divider="@null"
                android:dividerHeight="0dp"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:scrollbars="none"
                android:stackFromBottom="true"
                android:transcriptMode="normal"></ListView>
            <RelativeLayout
                android:id="@+id/Rl_MessageLayout"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="@color/chat_screen_bottom">

                <ImageButton
                    android:id="@+id/btn_Attachment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:background="@drawable/attachment_icon_white_2x" />

                <EditText
                    android:id="@+id/edt_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_toLeftOf="@+id/btn_Send"
                    android:layout_toRightOf="@+id/btn_Attachment"
                    android:background="@drawable/txt_field"
                    android:imeOptions="actionDone"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:singleLine="true"
                    />

                <ImageButton
                    android:id="@+id/btn_Send"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/icon_send_white_2x" />
            </RelativeLayout>
        </RelativeLayout>


    </RelativeLayout>

</RelativeLayout> 

這是我的活動代碼,用於計算listview的高度

private static boolean keyboardHidden = true;
    private static int reduceHeight =0;

    final View decorView = this.getWindow().getDecorView();
    decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            decorView.getWindowVisibleDisplayFrame(rect);
            int displayHeight = rect.bottom - rect.top;
            int height = decorView.getHeight();
            boolean keyboardHiddenTemp = (double) displayHeight / height > 0.8;
            int mylistviewHeight = Lv_Chat.getMeasuredHeight();
            if (keyboardHiddenTemp != keyboardHidden) {
                keyboardHidden = keyboardHiddenTemp;
                if (!keyboardHidden) {
                    reduceHeight = height - displayHeight;
                    RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mylistviewHeight - reduceHeight);
                    Lv_Chat.setLayoutParams(mParam);
                    Lv_Chat.requestLayout();
                } else {
                    RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mylistviewHeight + reduceHeight);
                    Lv_Chat.setLayoutParams(mParam);
                    Lv_Chat.requestLayout();
                }
            }
        }
    });

我也嘗試過此鏈接來設置xml文件http://codetheory.in/android-add-views-view-groups-listview-gridview/

任何幫助請

要更改ListView的高度,應使用LayoutParams

ViewGroup.LayoutParams param = listView.getLayoutParams();
param.height = anynumberhere;
listView.setLayoutParams(param);
listView.requestLayout();

根據您的問題,這是更改ListView的高度的方法,但是我不認為讓您的標頭保留在原位的儀式方法。

我認為您應該使用adjustResize而不是adjustNothing並在根RelativeLayout添加android:fitsSystemWindows="true" ,而不是通過編程方式嘗試調整視圖大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM