【问题标题】:Button not displayed Android按钮未显示 Android
【发布时间】:2014-04-11 03:53:17
【问题描述】:
我有一个线性布局和滚动条。我试图在滚动条下方有按钮。
你能建议我在这里做错了什么吗?
这是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/table_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
<Button android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:textColor="@android:color/background_light"
/>
</LinearLayout>
谢谢,
阿曼
【问题讨论】:
标签:
java
android
android-linearlayout
android-button
【解决方案1】:
通过将ScrollView 的权重设置为 1 并将高度设置为 0dp,它将包裹其高度以填充 Button 布局后剩余的所有空间以使用 wrap_content 高度:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="@+id/table_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
<Button
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:textColor="@android:color/background_light" />
</LinearLayout>
【解决方案2】:
添加
layout_weight
每个部分的参数(我添加了 90% 滚动视图 10% 按钮)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="9" >
<TableLayout
android:id="@+id/table_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
<Button android:id="@+id/next_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
</LinearLayout>
【解决方案3】:
您应该使用相对布局来代替 LinearLayout。这将解决一些问题
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/next_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:textColor="@android:color/background_light"
/>
<ScrollView
android:id="@+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/next_btn" >
<TableLayout
android:id="@+id/table_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
</RelativeLayout>