我正在尝试在项目中实现自定义导航栏片段,但是我无法设置标题和左键。在此片段中,我使用textview设置图标和标题。我知道我可以使用自定义工具栏,但我不想使用自定义工具栏,因此可以使用导航栏片段,并且可以在需要的任何地方将此导航片段用作工具栏,并设置向左按钮,向右键和所有 以下是我的NavigationBarFragment:-
class NavigationBarFragment : Fragment(), View.OnClickListener {
lateinit var leftButton: TextView
lateinit var titleTextView: TextView
private var buttonChangeListener: ButtonImageChangeListener? = null
interface ButtonImageChangeListener {
fun changeMenuButtonImage()
}
fun newInstance(title: String, isLeft: Boolean): NavigationBarFragment {
val navigationBarFragment = NavigationBarFragment()
val bundle = Bundle()
bundle.putString("title", title)
bundle.putBoolean("isLeft", isLeft)
navigationBarFragment.arguments = bundle
return navigationBarFragment
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_bar, container, false)
leftButton = view!!.findViewById(R.id.leftButton)
titleTextView = view!!.findViewById(R.id.title)
titleTextView.setText(arguments!!.getString("title"))
if (arguments!!.getBoolean("isLeft")) {
leftButton.visibility = View.VISIBLE
leftButton.setOnClickListener(this)
} else {
leftButton.visibility = View.GONE
}
}
interface navigationButtonListener {
fun leftButtonPressed()
}
private var listener: navigationButtonListener? = null
fun getListener(): navigationButtonListener? {
return listener
}
fun setListener(listener: navigationButtonListener) {
this.listener = listener
}
fun getButtonChangeListener(): ButtonImageChangeListener? {
return buttonChangeListener
}
fun setButtonChangeListener(buttonChangeListener: ButtonImageChangeListener) {
this.buttonChangeListener = buttonChangeListener
}
override fun onClick(v: View?) {
if (v != null) {
if (v.getId() == R.id.title) run {
} else if (v!!.getId() == R.id.leftButton) {
if (this.listener != null) {
this.listener!!.leftButtonPressed()
}
}
}
}
}
下面是我在Activity中实现的方法:
private var navigationBar: NavigationBarFragment? = null
private fun loadNavigationbar() {
navigationBar = NavigationBarFragment().newInstance("Report",true)
navigationBar?.setListener(this)
navigationBar?.setButtonChangeListener(object : NavigationBarFragment.ButtonImageChangeListener {
override fun changeMenuButtonImage() {
//title
navigationBar!!.titleTextView.setTextColor(resources.getColor(R.color.colorPrimary))
//navigationBar!!.titleTextView.setTypeface(Application.HelveticaNeue_Medium)
var aa= navigationBar!!.titleTextView.text
navigationBar!!.titleTextView.setText("Report Activity")
//Menu Button
navigationBar!!.leftButton.setText(resources.getResourceName(R.drawable.ic_navigate_back))
// navigationBar.leftButton.setTypeface(Application.FONT_AWESOME)
navigationBar!!.leftButton.setTextSize(24F)
navigationBar!!.leftButton.setTextColor(resources.getColor(R.color.colorPrimary))
navigationBar!!.leftButton.setPadding(15, 5, 15, 5)
navigationBar!!.leftButton.setVisibility(View.VISIBLE)
navigationBar!!.navigationBarBaseLayout.setBackgroundColor(resources.getColor(R.color.white))
val ssbProfile = SpannableStringBuilder(" ")
ssbProfile.setSpan(ImageSpan(this@ReportActivity, R.drawable.ic_navigate_back), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
navigationBar!!.leftButton.setText(ssbProfile, TextView.BufferType.SPANNABLE)
navigationBar!!.leftButton.setTextSize(32F)
navigationBar!!.leftButton.setTextColor(resources.getColor(R.color.colorPrimary))
navigationBar!!.leftButton.setPadding(15, 5, 15, 5)
}
})
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.navigationBar, this!!.navigationBar!!)
fragmentTransaction.commit()
}
以下是我的xml代码:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigationBarBaseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="C"
android:textColor="#FFFFFF"
android:textSize="48dp" />
<TextView
android:id="@id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:maxLength="30"
android:singleLine="true"
android:text="CATELOGUE"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/rightFirstButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/rightSecondButton"
android:text="c"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/rightSecondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/rightThirdButton"
android:text="c"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/rightThirdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="c"
android:textColor="#FFFFFF"
android:textSize="20dp" />
答案 0 :(得分:0)
我如何解决这个问题,但是在Java中不是kotlin。这适用于左按钮,适用于右按钮,与左按钮相同。如果有人在Kotlin中找到解决方案,请让我知道我会接受他的回答。
下面是我的NavigationBar代码:
public class NewNavigationBarFragment extends Fragment implements View.OnClickListener {
public TextView leftButton;
public TextView titleTextView;
public RelativeLayout navigationBarBaseLayout;
private ButtonImageChangeListener buttonChangeListener;
public interface ButtonImageChangeListener {
void changeMenuButtonImage();
}
public interface navigationButtonListener {
void leftButtonPressed();
}
private navigationButtonListener listener;
public navigationButtonListener getListener() {
return listener;
}
public void setListener(navigationButtonListener listener) {
this.listener = listener;
}
public ButtonImageChangeListener getButtonChangeListener() {
return buttonChangeListener;
}
public void setButtonChangeListener(ButtonImageChangeListener buttonChangeListener) {
this.buttonChangeListener = buttonChangeListener;
}
public NewNavigationBarFragment() {
// Required empty public constructor
}
public static NewNavigationBarFragment newInstance(String title, boolean isLeft) {
NewNavigationBarFragment navigationBarFragment = new NewNavigationBarFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putBoolean("isLeft", isLeft);
navigationBarFragment.setArguments(bundle);
return navigationBarFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_navigation_bar, container, false);
leftButton = view.findViewById(R.id.leftButton);
titleTextView = view.findViewById(R.id.title);
navigationBarBaseLayout = (RelativeLayout) view.findViewById(R.id.navigationBarBaseLayout);
if (getArguments().getBoolean("isLeft")) {
leftButton.setVisibility(View.VISIBLE);
leftButton.setOnClickListener(this);
} else {
leftButton.setVisibility(View.GONE);
}
if (buttonChangeListener != null) {
buttonChangeListener.changeMenuButtonImage();
}
return view;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.title) {
} else if (v.getId() == R.id.leftButton) {
if (this.listener != null) {
this.listener.leftButtonPressed();
}
}
}
}
在活动中调用以下代码,不要忘了implement
导航栏listener
并全局声明navigationBar
私人娱乐loadNavigationbar(){ navigationBar = NewNavigationBarFragment.newInstance(“”,true)
navigationBar!!.setListener(this)
navigationBar!!.setButtonChangeListener(object : NewNavigationBarFragment.ButtonImageChangeListener {
override fun changeMenuButtonImage() {
//title
navigationBar!!.titleTextView.setText("Reports")
// navigationBar!!.titleTextView.setTypeface(Application.HelveticaNeue_Medium)
navigationBar!!.navigationBarBaseLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
navigationBar!!.titleTextView.setTextColor(resources.getColor(R.color.white))
val ssbProfile = SpannableStringBuilder(" ")
ssbProfile.setSpan(ImageSpan(this@ReportActivity, R.drawable.ic_navigate_back), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
navigationBar!!.leftButton.setText(ssbProfile, TextView.BufferType.SPANNABLE)
navigationBar!!.leftButton.setTextSize(32F)
navigationBar!!.leftButton.setTextColor(resources.getColor(R.color.colorPrimary))
navigationBar!!.leftButton.setPadding(15, 5, 15, 5)
navigationBar!!.leftButton.setVisibility(View.VISIBLE);
}
})
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.navigationBar, navigationBar!!)
fragmentTransaction.commit()
}