Java 可以在弹出窗口中使用ActionBar吗
在扩展ListFragment的类中,我有一个弹出窗口,当我单击一个按钮时会显示该窗口。我的问题是我不知道我是否可以在里面使用ActionBar。我的意思是,我想得到一个返回箭头,当它被点击时,它应该关闭(关闭)弹出窗口。我的意思是:像这样的东西:Java 可以在弹出窗口中使用ActionBar吗,java,android,popupwindow,android-listfragment,android-popupwindow,Java,Android,Popupwindow,Android Listfragment,Android Popupwindow,在扩展ListFragment的类中,我有一个弹出窗口,当我单击一个按钮时会显示该窗口。我的问题是我不知道我是否可以在里面使用ActionBar。我的意思是,我想得到一个返回箭头,当它被点击时,它应该关闭(关闭)弹出窗口。我的意思是:像这样的东西: toolbar = findViewById(R.id.tool_bar); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Enter t
toolbar = findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Enter transaction");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
这样的功能可能吗?或者更确切地说,我应该使用添加了ImageView的自定义工具栏使用set listener关闭弹出窗口吗?或者我应该创建自定义对话框
我附上一个文件,它显示了我现在拥有的和我想要实现的。
这是我班级的一部分
public class LastTransactionsFragment extends ListFragment implements, View.OnClickListener{
//popupWindow
private View popupView;
private Button chooseTransactionDate;
private Button chooseTodayTomorrow;
private RadioGroup chooseTransactionType;
private RadioButton chosenTransactionExpense;
private RadioButton chosenTransactionIncome;
private ImageView transactionCategoryImage;
private Spinner chooseTransactionCategory;
private EditText chooseTransactionAmount;
private EditText chooseTransactionName;
private Button saveTransactionButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_last_transactions, container, false);
addTransactionActionButton = view.findViewById(R.id.add_transaction_action_button);
addTransactionActionButton.setOnClickListener(this);
return view;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_transaction_action_button: {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_transaction, null);
PopupWindow popupWindow = new PopupWindow(popupView, 565, 760, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Toolbar toolbar = popupView.findViewById(R.id.tool_bar);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setTitle("New transaction");
int currentDay = 1;
int currentMonth = Integer.parseInt(actualChosenMonth.getText().toString().substring(0, 2)) - 1;
int currentYear = Integer.parseInt(actualChosenMonth.getText().toString().substring(3, 7));
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE");
Date date = new Date(currentYear, currentMonth, currentDay - 1);
String currentDayOfWeek = simpledateformat.format(date);
String currentDateAsString = convertDateToString(currentDay, currentMonth, currentYear, currentDayOfWeek);
chooseTransactionDate = popupView.findViewById(R.id.chooseTransactionDate);
chooseTransactionDate.setText(currentDateAsString);
chooseTransactionDate.setOnClickListener(this);
//I cut rest of of listener
saveTransactionButton = popupView.findViewById(R.id.saveTransactionButton);
saveTransactionButton.setOnClickListener(this);
break;
}
default:
break;
}
}
这是我的布局文件(带有自定义工具栏)
//我剪切了布局的其余部分
我解决了我的问题。我用了这个:
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
有人能帮我吗?
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);