java - 我如何将 BottomNavigationView 放在 <layout> 中?

标签 java android xml android-studio bottomnavigationview

正如您可能猜到的那样,我是 Android Studio 的初学者。 我试图在布局中放置一个 BottomNavigationViewSearchView 需要它,但似乎无法解决。 这是我能做的最好的,但 BottomNavigationView 仍然没有出现:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true" />
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="450dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/search" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        menu="@menu/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        />
</RelativeLayout>
</layout>

如果有人有任何想法,我很乐意得到一些帮助,在此先感谢!

编辑:这是 Activity - (我将 xml 的名称 activity_main.xml 更改为以便允许我使用绑定(bind))

public class SearchUsers extends AppCompatActivity {
private ActivityMainBinding binding;
ListAdapter adapter;
List<String> arrayList= new ArrayList<>();
private SearchView sv;
private ListView lv;
private MobileServiceClient mService=null;

private MobileServiceTable<users> mTable=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    sv=(SearchView) findViewById(R.id.search);
    lv=(ListView)findViewById(R.id.list_view);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    try
    {
        mService= new MobileServiceClient("https://beyondapplication.azurewebsites.net",SearchUsers.this);

        mTable=mService.getTable(users.class);
    }
    catch (Exception e)
    {
        Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
    }

    AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
        @Override
        protected Boolean doInBackground(Void... voids) {
            try {
                List<users> myuser = mTable.execute().get();

                    if (myuser.size() != 0) {
                        for (int i=0; i< myuser.size();i++)
                        {
                            arrayList.add(myuser.get(i).getName());

                        }

                        return true;
                } else

                    return false;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean answer) {
            super.onPostExecute(answer);

            if (answer == true) {


            } else {

                //    finish();

            }

        }
    }.execute();



    adapter= new MyAdapter(arrayList);

    binding.listView.setAdapter(adapter);
    binding.search.setActivated(true);
    binding.search.setQueryHint("search by username");
    binding.search.onActionViewExpanded();
    binding.search.setIconified(false);
    binding.search.clearFocus();

    binding.search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {


            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            ((MyAdapter) adapter).getFilter().filter(newText);

            //String s=binding.search.getQuery().toString();
           // if ((binding.search.getQuery().toString().equals("") || binding.search.getQuery().toString().equals(null)|| binding.search.getQuery().toString().equals(" ") && arrayList.size()>0))

            return false;
        }

    });
    binding.search.setActivated(true);
    binding.search.setQueryHint("search by username");
    binding.search.onActionViewExpanded();
    binding.search.setIconified(false);
    binding.search.clearFocus();

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
            {
                return true;
            }
            case R.id.profile:
            {


            }
            return true;
            case R.id.navigation_notifications:
            {
                return true;

            }
            case R.id.find:
                startActivity(new Intent(SearchUsers.this, SearchUsers.class));
                overridePendingTransition(0, 0);
                return true;
        }
        return false;
    }
};

最佳答案

它是工作代码:

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

    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/navigation"
        android:layout_below="@+id/search" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />
</RelativeLayout>

enter image description here

关于java - 我如何将 BottomNavigationView 放在 <layout> 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52634037/

相关文章:

java - Hadoop Map/Reducer 类无法显示任何内容

java - JComboBox 在 GridBagLayout 中插入后更改 JTextField 宽度

java - 如何从java中的.class文件获取实例

java - 无法在 Android Studio/Java 中将字符串转换为整数

PHP -> 如何检查 XML 是否为空

java - 我在哪里可以找到 Axis WSDL2Java 文件?

android - Okhttp 在与 Retrofit RxJavaCallAdapterFactory 一起使用时忽略 Dispatcher 设置

android - fatal error : openssl/evp. h 没有这样的文件或目录

java - 使用 GWT 使用 XMLHttpRequest 发送 XML

xml - 如何将一些 XML 元素包含在边界框中?