【问题标题】:Problems getting a LinearLayout to stay on the bottom - Android让 LinearLayout 保持在底部的问题 - Android
【发布时间】:2012-01-30 09:55:22
【问题描述】:

我有一个固定在顶部的操作栏,一个包含主要 UI 内容的 ScrollView,以及一个我想保留在屏幕底部的页脚(但我无法做到)。注意:我为调试设置了一个绿色和另一个红色的布局。

XML 方面,我有一个基本 XML 文件,稍后会填充:

base_layout.xml:

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <include layout="@layout/actionbar" />

   <ScrollView
      android:orientation="vertical"
      android:fillViewport="true"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >

      <LinearLayout
         android:id="@+id/main_menu_layout"
         android:orientation="vertical"
         android:background="#00FF00"  <!-- Note: GREEN -->
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
      </LinearLayout>
   </ScrollView>
</LinearLayout>

有些活动会有 PrefsBar 而有些没有,这就是为什么我在 base_layout.xml 文件中没有“include layout="@layout/prefs_bar"”(见下文)......以及为什么我没有使用RelativeLayout。

在我的活动中,我执行以下操作:

 public void onCreate(Bundle savedInstanceState)
 {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.base_layout);

      LinearLayout emptyMainLayout = (LinearLayout)findViewById(R.id.main_menu_layout);
      LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View menuRowLayout = inflater.inflate(R.layout.tools_kvar, null);
      emptyMainLayout.addView(menuRowLayout);

tools_kvar.xml 文件包含:

 <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FF0000" >   <!-- Note: RED -->

      <ScrollView
          android:orientation="vertical"
          android:fillViewport="true"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >

         <TableLayout
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:stretchColumns="2">
             < .... ETC .... >
         </TableLayout>
      </ScrollView>

      <include layout="@layout/prefs_bar" />
 </LinearLayout>

第一个问题:为什么 tools_kvar.xml (RED) 中的主要 LinearLayout 没有填充所有外部布局? android:layout_height="fill_parent" 设置好了!

prefs_bar.xml(见上文)是我需要固定在屏幕底部的内容。它包含:

 <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/prefs_bar_layout"
      android:orientation="horizontal"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:background="@color/evengrayer"
      android:gravity="bottom"
      android:layout_gravity="bottom">

      <TextView  ... ETC ...
 </LinearLayout>

目前的结果是这样的:

任何帮助将不胜感激!

【问题讨论】:

  • 为什么不使用Relative layout 而应用android:layout_alignParentBottom = "true" 属性?
  • 有些活动会有 PrefsBar 而有些没有,这就是为什么我在 base_layout.xml 文件中没有“include layout="@layout/prefs_bar"”(见下文) ...以及为什么我没有使用RelativeLayout。一个解决方案是创建两个“base_layout.xml”文件,一个带有首选项栏,另一个没有,我猜。
  • 是的。这似乎是一个可能的解决方案。我看不到使用线性布局将您的 PrefsBar 固定在底部的方法。但是您希望实现的 UI 可以仅使用相对布局进行设置。如果您能详细说明为什么要坚持使用线性布局和滚动视图,我认为这会有所帮助。

标签: android scrollview android-linearlayout gravity


【解决方案1】:

尝试使用给定的代码块更改 tools_kvar.xml 以用于滚动视图

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="2" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="test" />
    </TableLayout>
</ScrollView>

【讨论】:

  • 刚刚尝试过,但没有任何变化。我想我会选择 RelativeLayout =[
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多