1 回答
TA贡献1862条经验 获得超6个赞
minHeight 属性是回答这个问题的关键。
我首先得到应用程序屏幕大小,然后从中减去按钮和工具栏大小。然后我将其设置为我的内容所在的线性布局的最小尺寸。
我使用了 onWindowFocusChanged 函数,因为有时布局可能需要时间才能完全加载,因此获取按钮高度可能会失败。
@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);
}
这是经过一些更改后的布局代码。
<?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>
添加回答
举报