Android 如何将操作按钮添加到操作栏
我是Android开发的新手。 如果可能的话,有人能解释一下如何将action button添加到这个类中吗Android 如何将操作按钮添加到操作栏,android,Android,我是Android开发的新手。 如果可能的话,有人能解释一下如何将action button添加到这个类中吗 public class HomeFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { HomeViewMode
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
公共类HomeFragment扩展了片段{
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
HomeViewModel HomeViewModel=ViewModelProviders.of(this.get)(HomeViewModel.class);
视图根=充气机。充气(R.layout.fragment\u home,container,false);
final TextView TextView=root.findviewbyd(R.id.text\u home);
homeViewModel.getText().observe(这是新的观察者(){
@凌驾
公共void onChanged(@Nullable String s){
textView.setText;
}
});
返回根;
}
和xml文件
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
在活动中重写此方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
为此,您必须在菜单文件夹中创建一个名为“home”的菜单(如果您没有看到菜单文件夹,请在res中创建一个)在活动中覆盖此方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
为此,您必须在菜单文件夹中创建一个名为“主页”的菜单(如果您没有看到菜单文件夹,请在res中创建一个)如果要在工具栏上添加操作按钮,您必须:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="Add"
app:showAsAction="ifRoom|collapseActionView"/>
</menu>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_add){
//Do something
}
return super.onOptionsItemSelected(item);
}
我希望这将对您有所帮助!如果您想在工具栏上添加操作按钮,您必须:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="Add"
app:showAsAction="ifRoom|collapseActionView"/>
</menu>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_add){
//Do something
}
return super.onOptionsItemSelected(item);
}
我希望这将对您有所帮助!如果您想自定义操作栏的U.I,那么您可以使用工具栏作为操作栏。以下是执行相同操作的步骤: //创建一个操作栏按钮 @凌驾 CreateOptions菜单(菜单)上的公共布尔值{ //R.menu.mymenu是对名为mymenu.xml的xml文件的引用,该文件应位于res/menu目录中 //如果没有res/menu,只需在res中创建一个名为“menu”的目录
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}如果要自定义操作栏的U.I,则可以将工具栏用作操作栏。以下是执行相同操作的步骤: //创建一个操作栏按钮 @凌驾 CreateOptions菜单(菜单)上的公共布尔值{ //R.menu.mymenu是对名为mymenu.xml的xml文件的引用,该文件应位于res/menu目录中 //如果没有res/menu,只需在res中创建一个名为“menu”的目录
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}以这种方式实现mymenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/mybutton"
android:title="Add"
app:showAsAction="always"
android:icon="@drawable/mybuttonicon"
/>
</menu>
以这种方式实现mymenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/mybutton"
android:title="Add"
app:showAsAction="always"
android:icon="@drawable/mybuttonicon"
/>
</menu>
要添加OnCreateOptions菜单,应按CTRL+O打开对话框窗口,然后键入OnCreateOptions菜单并选择它 让Android Studio在相关和必要的位置创建代码。(来源:)
因此,您可能会在错误的参数上编写正确的代码。一旦您想要添加OnCreateOptions菜单,您应该按CTRL+O打开对话框窗口,然后键入OnCreateOptions菜单并选择它 让Android Studio在相关和必要的位置创建代码。(来源:)
因此,您可能会在错误的偏执上编写真正的代码。您真正想要做什么?在工具栏中添加操作按钮,但在哪里?…您真正想要做什么?在工具栏中添加操作按钮,但在哪里。。。