kal*_*lik 8 java android android-layout android-view bottomnavigationview
我有一个BottomNavigationView我像这样实例化的:
BottomNavigationView navigationView = findViewById(R.id.bottom_navigation);
navigationView.setOnNavigationItemSelectedListener(this);
Run Code Online (Sandbox Code Playgroud)
/menu/menu.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/first_id"
android:icon="@drawable/home_icon"
android:title="Item" />
<item
android:id="@+id/second_id"
android:icon="@drawable/home_icon"
android:title="Item" />
<item
android:id="@+id/third_id"
android:icon="@drawable/home_icon"
android:title="Item" />
<item
android:id="@+id/fourth_id"
android:icon="@drawable/home_icon"
android:title="Item" />
</menu>
Run Code Online (Sandbox Code Playgroud)
/drawable/home_icon 看起来像这样:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FFFFF0"
android:pathData="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"
android:strokeColor="#FFFFF0"
android:strokeWidth="1" />
</vector>
Run Code Online (Sandbox Code Playgroud)
如您所见,导航项的按钮周围有边框,但是,边框相互接触。因此我想在菜单按钮之间添加一个边距。我怎样才能做到这一点?
从屏幕截图中也可以看出,我正在阻止切换模式。我通过调用来做到这一点
BottomNavigationViewHelper.removeShiftMode(navigationView);
Run Code Online (Sandbox Code Playgroud)
使用这里的代码。
按钮周围的边框已添加如下:
int stateListDrawable = drawableStateLists.get(currentFragment);
navigationView.setItemBackgroundResource(stateListDrawable);
Run Code Online (Sandbox Code Playgroud)
whilestateListDrawable指的是StateListDrawable当按下不同的菜单按钮时会发生变化的 a。的StateListDrawable指向XML选择这又是指一个xml形状像这样,例如:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/>
<stroke
android:width="2dp"
android:color="@color/intro"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
azi*_*ian 10
在仔细检查和试验来源之后,我想出了最基本和合法的解决方案 - 插入可绘制对象本身。
像这样声明了 xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="10dp"
android:insetRight="10dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#ffffff" />
</shape>
</inset>
Run Code Online (Sandbox Code Playgroud)
你会得到以下输出:
我努力首先找到一个程序化的解决方案,但事实证明不存在那么多接缝来做到这一点(至少这是我的发现)。准确地说,每个项目的布局参数都是ViewGroup.LayoutParams. 如果项目使用 进行布局ViewGroup.MarginLayoutParams,则可以单独为每个项目设置边距。在此之前,应将背景可绘制 xml 应用到 inset 中。
| 归档时间: |
|
| 查看次数: |
4933 次 |
| 最近记录: |