有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android内容隐藏的java底部导航

在我第一次尝试Android(Studio 3.0)时,我成功地实现了bottom navigationfragments

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

demonstration

该片段由两个布局、一个listview和两个按钮组成:

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

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

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


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

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

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

如您所见,我正在使用安卓:layout_alignBottom="@id/lvTransportLines"将LinearLayout(保留两个按钮)放置在Listview下方。 我之所以能看到这些按钮,是因为我把它们的高度设置为某个巨大的东西,以供测试之用

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

更新 在实施@Nero的解决方案后,我想到了以下几点:

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

    >

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

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

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

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

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

但我得到的结果和第一张照片完全一样。我做错了什么

主要活动。xml

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

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

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

</安卓.support.constraint.ConstraintLayout>

共 (2) 个答案

  1. # 1 楼答案

    将其添加到相对布局或框架布局中

    android:layout_marginBottom="?actionBarSize"> 
    
  2. # 2 楼答案

    抱歉耽搁了,伙计,我在测试一些东西,不得不从头开始创建这个例子。有趣的是,你只需要在你的主要活动中对框架布局进行一次修改。xml文件

    请添加以下属性,它将为您提供所需的设计

    android:layout_marginBottom="?actionBarSize"
    

    按要求更新

    主要活动。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" />
    

    碎片是空白的。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>
    

    主要活动。爪哇

    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