1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
minHeight 屬性是回答這個(gè)問(wèn)題的關(guān)鍵。
我首先得到應(yīng)用程序屏幕大小,然后從中減去按鈕和工具欄大小。然后我將其設(shè)置為我的內(nèi)容所在的線性布局的最小尺寸。
我使用了 onWindowFocusChanged 函數(shù),因?yàn)橛袝r(shí)布局可能需要時(shí)間才能完全加載,因此獲取按鈕高度可能會(huì)失敗。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Configuration configuration = getResources().getConfiguration();//returns the app screen size.
int screenHeightDp = configuration.screenHeightDp-56;// 56 is the max height of the android toolbar.
float density = this.getResources().getDisplayMetrics().density;
screenHeightDp = (int)(screenHeightDp*density);//changes the dp to pixel
Button perWordHistory = findViewById(R.id.perWordHistory);
int heightOfButton = perWordHistory.getHeight()*2; // as I have two buttons
screenHeightDp = screenHeightDp - heightOfButton;
LinearLayout linearLayout = findViewById(R.id.layoutContainingRecycleView);
linearLayout.setMinimumHeight(screenHeightDp);
}
這是經(jīng)過(guò)一些更改后的布局代碼。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutContainingRecycleView">
<android.support.v7.widget.RecyclerView
android:id="@+id/eachWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/perWordHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_history" />
<Button
android:id="@+id/Help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/help" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
添加回答
舉報(bào)