java - Android 上的底部导航 - 内容隐藏

标签 java android android-layout android-fragments android-studio-3.0

我设法实现了 bottom navigationfragments在我第一次尝试使用 Android(Studio 3.0)时。

现在,在第一个 fragment 上实现小部件时,我意识到底部导航覆盖/隐藏了 fragment 的最下方部分。

demonstration

Fragment 包含两个布局,一个 ListView 和两个按钮:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.xyz.ticketvendor.QuickRideFragment">

<!-- TODO: Update blank fragment layout -->


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

    <ListView
        android:id="@+id/lvTransportLines"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignBottom="@id/lvTransportLines">
        <Button
            android:id="@+id/getTicket"
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_weight=".75"
            android:text="Get Ticket" >
        </Button>
        <Button
            android:id="@+id/setDefaultSMSApp"
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_weight=".25"
            android:text="Reset" >
        </Button>
    </LinearLayout>
</RelativeLayout>

如您所见,我正在使用 android:layout_alignBottom="@id/lvTransportLines" 将 LinearLayout(保留 2 个按钮)定位在 Listview 下方。 我只能看到按钮,因为我出于测试目的将它们的高度设置为巨大的东西。

但是 - 我可以使用哪个布局属性将 LinearLayout 定位得足够高,以免被底部导航隐藏?谢谢!

更新 在实现@Nero 的解决方案后,我想到了这个:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.antibala.ticketvendor.QuickRideFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"

    >

    <ListView
        android:id="@+id/lvTransportLines"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"

        >
        <Button
            android:id="@+id/getTicket"
            android:layout_width="0dp"
            android:layout_height="140dp"
            android:layout_weight=".75"
            android:text="Get Ticket"

            >
        </Button>
        <Button
            android:id="@+id/setDefaultSMSApp"
            android:layout_width="0dp"
            android:layout_height="140dp"
            android:layout_weight=".25"
            android:text="Reset"

            >
        </Button>
    </LinearLayout>
</RelativeLayout>

但我得到的结果与第一张照片完全相同。我做错了什么?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xyz.ticketvendor.MainActivity">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

最佳答案

抱歉耽误了伙伴,我正在测试一些东西,不得不从头开始创建这个例子。有趣的是,您只需对 Main_activity.xml 文件中的 FrameLayout 进行一次修改。

请添加以下属性,它应该会为您提供您想要的设计。

android:layout_marginBottom="?actionBarSize"

按要求更新

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nero.myapplication.MainActivity">

<RelativeLayout
    android:id="@+id/fragment_Container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="?actionBarSize"></RelativeLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

Fragment_blank.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nero.myapplication.BlankFragment">



<LinearLayout
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/getTicket"
        android:layout_width="0dp"
        android:layout_height="140dp"
        android:layout_weight=".75"
        android:text="Get Ticket"/>
    <Button
        android:id="@+id/setDefaultSMSApp"
        android:layout_width="0dp"
        android:layout_height="140dp"
        android:layout_weight=".25"
        android:text="Reset"/>

</LinearLayout>


<ListView
    android:id="@+id/lvTransportLines"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/buttons"/>
</RelativeLayout>

主要 Activity .java

public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);
                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.fragment_Container, new BlankFragment(), "blankFragment")
            .disallowAddToBackStack()
            .commit();
}

Result

关于java - Android 上的底部导航 - 内容隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480176/

相关文章:

java - IntentService 中未调用 OnHandleIntent()

java - Android Studio,Java : Cannot change horizontal margin at run time (ConstraintSet. 左)

java - 如何减少向远程服务器发送数据的时间?

android - 将具有 xml 布局的 fragment 动态添加到 GridLayout 不起作用

Android 自定义 ArrayAdapter 不停留在滚动条上

java - 加载 postgreSQL JDBC 驱动程序

java - 线程中出现异常 "AWT-EventQueue-1"java.lang.NullPointerException 错误

java - 如何从 Java 运行 Node.js 进程?

java - 在android中定期运行异步任务

android - 如何在android的默认浏览器选择列表中添加我的浏览器?