android - 根据固定页脚限制 fragment 大小

标签 android android-layout android-fragments android-actionbar

demo design

我正在根据上述设计创建一个 Android 应用程序。我制作了一个自定义操作栏并将其粘贴到我的 MainActivity 中的 onCreateOptionsMenu。

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);

我还在我的 MainActivity 中创建了一个固定的页脚,它基本上是用于触发应用程序中不同 fragment 的 ImageView 。现在这些 ImageView 的高度设置为 wrap_content。

我的问题是-

  1. 如何限制 fragment 的大小,使其在底部没有特定高度的情况下固定在固定的 View 组中?
  2. 这是实现这种有固定页脚以在 fragment 之间交换的设计的最佳方法吗?
  3. 如何创建和添加类似于 Google 卡片之类的可重复使用的 UI 组件,我可以在其中从服务器推送数据并将这些数据动态包含在 ScrollView 中。

谢谢。

编辑 1

activity_main -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dd.MainActivity" >

        <!-- Footer aligned to bottom -->
        <LinearLayout
            android:id="@+id/llFooterPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >

            <View
                android:layout_width="fill_parent"
                android:layout_height="3dp"
                android:background="@color/green" />

            <LinearLayout
                android:id="@+id/llFooterMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/bottom_bar"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/iv1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:scaleType="fitCenter"
                    android:src="@drawable/state_definition_iv1" />

                <ImageView
                    android:id="@+id/iv2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:scaleType="fitCenter"
                    android:src="@drawable/state_definition_iv2" />

                <ImageView
                    android:id="@+id/iv3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:background="@color/green"
                    android:scaleType="center"
                    android:src="@drawable/state_definition_iv3" />

                <ImageView
                    android:id="@+id/iv4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:scaleType="fitCenter"
                    android:src="@drawable/state_definition_iv4" />

                <ImageView
                    android:id="@+id/iv5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:scaleType="fitCenter"
                    android:src="@drawable/state_definition_iv5" />
            </LinearLayout>
        </LinearLayout>

        <!-- Scrollable item above footer -->
        <ScrollView
            android:id="@+id/svContent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/llFooterPlaceholder"
            android:layout_alignParentTop="true" >

            <!-- Inflate the contents of the ScrollView dynamicaly -->
        </ScrollView>
</RelativeLayout>

fragment1 在点击页脚的 iv1 时激活(布局尚未完成)-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:background="#444"
        android:orientation="horizontal"
        android:padding="@dimen/item_padding" >

        <ImageView
            android:id="@+id/ivAvtar"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="30"
            android:background="#000"
            android:scaleType="fitCenter" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:background="#fff"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvUsername"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:background="#0ff"
                android:text="Username"
                android:textColor="@color/green"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvImageTitle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:background="#ff0"
                android:text="Title"
                android:textColor="@color/orange"
                android:textStyle="italic" />
        </LinearLayout>

        <TextView
            android:id="@+id/tvTimestamp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20"
            android:gravity="center"
            android:text="Timestamp"
            android:textColor="@color/green"
            android:textStyle="bold" />
    </LinearLayout>

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60"
        android:background="#999"
        android:padding="@dimen/item_padding"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="#EEE"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="80"
            android:background="#000"
            android:orientation="horizontal" >
        </LinearLayout>

        <ImageView
            android:id="@+id/ivShare"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20"
            android:background="#ddd"
            android:padding="@dimen/item_padding"
            android:scaleType="fitCenter" />
    </LinearLayout>

</LinearLayout>

最佳答案

ScrollView 高度从 fill_parent 更改为 wrap_content。因为 android:layout_height="fill_parent" 你的 ScrollView 覆盖了你的 llFooterPlaceholder

像这样

<ScrollView
        android:id="@+id/svContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/llFooterPlaceholder"
        android:layout_alignParentTop="true" >

        <!-- Inflate the contents of the ScrollView dynamicaly -->
</ScrollView>

关于android - 根据固定页脚限制 fragment 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25720324/

相关文章:

java - 当我滚动时,RecyclerView 设置了错误的项目高度

android - SwipeDismissBehavior 动画问题

android - 在 Android Studio 中为 android 应用程序设置开发和生产环境

java - 安卓如何删除文件?

android - 用于 fragment 的 JUnit

Android中TabLayout、ViewPager Fragments更新不一致

android - 通过删除前一个 fragment 来替换当前 fragment

android - 线上/线下形象管理策略

Android Studio 3.0 xml-layout 文件的预览未显示

安卓 SearchView OnFocusChangeListener : onFocusChange is not called at all