Java android如何更改工具栏标题、图像和背景颜色
这是片段布局Java android如何更改工具栏标题、图像和背景颜色,java,android,Java,Android,这是片段布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:ap
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/topbar"
android:layout_width="match_parent"
android:background="@color/black"
android:layout_height="@dimen/_60sdp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bookappointment"
android:fontFamily="@string/roboto_medium"
android:textColor="#ffffff"
android:textSize="@dimen/_20sdp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<android.support.v7.widget.CardView
android:id="@+id/dameklipCard"
android:layout_width="wrap_content"
android:layout_height="@dimen/_180sdp"
android:layout_margin="@dimen/_10sdp"
android:layout_below="@+id/topbar"
app:cardCornerRadius="@dimen/_15sdp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#f5f5ef"
card_view:cardElevation="3dp"
app:cardCornerRadius="@dimen/_15sdp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rounderblue"
android:layout_width="match_parent"
android:layout_height="@dimen/_120sdp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/cardImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_bg"/>
<TextView
android:id="@+id/services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:fontFamily="sans-serif-light"
android:textStyle="italic"
android:textSize="@dimen/_11sdp"
android:textColor="#EAEAEA"
android:layout_marginTop="@dimen/_16sdp"
android:text="@string/services"/>
<TextView
android:id="@+id/serviceTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/services"
android:layout_marginStart="@dimen/_16sdp"
android:textSize="@dimen/_25sdp"
android:textColor="#EAEAEA"
android:text=""/>
<TextView
android:id="@+id/durationTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/serviceTv"
android:layout_marginStart="@dimen/_16sdp"
android:fontFamily="@string/roboto_genraltext"
android:textSize="@dimen/_11sdp"
android:textColor="#FFFFFF"
android:text=""/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/stylistImg"
android:layout_width="@dimen/_74sdp"
android:layout_height="@dimen/_74sdp"
android:layout_marginTop="@dimen/_16sdp"
android:layout_marginEnd="@dimen/_30sdp"
android:src="#90eeeded"
android:layout_alignParentEnd="true"
/>
<TextView
android:id="@+id/stylistNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/stylistImg"
android:layout_alignParentEnd="true"
android:textSize="@dimen/_11sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_40sdp"
android:textColor="@color/white"
android:textStyle="bold"
android:layout_marginBottom="@dimen/_10sdp"
android:text="" />
</RelativeLayout>
<TextView
android:id="@+id/timeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rounderblue"
android:fontFamily="@string/roboto_light"
android:layout_marginStart="@dimen/_16sdp"
android:textColor="#383838"
android:text="@string/your_appointment_date_time"
android:textSize="@dimen/_15sdp"
android:textStyle="italic" />
<TextView
android:id="@+id/dateTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timeTV"
android:layout_marginTop="@dimen/_5sdp"
android:text=""
android:textColor="#000000"
android:textSize="@dimen/_18sdp"
android:textStyle="bold"
android:layout_marginStart="@dimen/_16sdp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/stylistRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_below="@+id/dameklipCard"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
这是我的片段
public class StylistFragment extends Fragment {
private RecyclerView recyclerView;
String docid;
CircleImageView stylistImg;
public StylistFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.stylist_recycler, container, false);
recyclerView=view.findViewById(R.id.stylistRecycler);
stylistImg=view.findViewById(R.id.stylistImg);
Bundle bundle=getArguments();
if(bundle!=null){
final String id=bundle.getString("userdocumentid","");
docid=id;
Log.d("selectedDocumentId",id);
}
try{
FirebaseFirestore db = FirebaseFirestore.getInstance();
try {
db.collection("stylist").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
try {
ArrayList<String> stylistId=new ArrayList<>();
for(DocumentSnapshot documentSnapshot:task.getResult()){
String id=documentSnapshot.getId();
stylistId.add(id);
}
StylistAdapter listAdapter = new StylistAdapter(task,docid,stylistId,stylistImg);
RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(getContext(), R.drawable.divider));
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
listAdapter.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
return view;
}
}
当我选择底部导航项并将其重定向到相应的片段时,我的项目中有五个片段。但问题是,当我更改片段时,工具栏正在刷新。我只想更改标题,工具栏的图像和背景色。我不想更改工具栏,它应该是相同的,只是我想更改标题、图像和背景色。请尝试下面的代码可能会对您有所帮助
ColorDrawable mColorDrawable = new ColorDrawable(Color.parseColor("#000000"));
getActivity().getActionBar().setBackgroundDrawable(mColorDrawable);
getActivity().getActionBar().setTitle("Alert");
否则
请使用工具栏并设置自定义布局,如下面的示例所示
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarM"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorDarkBlue"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:id="@+id/rlTopBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolBarTitle"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="1"
android:singleLine="true"
android:text="Text Title"
android:textColor="@color/white"
android:textSize="@dimen/font_very_large"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
共享您的java文件使用getActivity.setTitletitle;对于图像和背景,可以使用getActivity.setActionBarToolbar;如果我想把标题放在工具栏的中心,这意味着我必须要做的事情。是的,我可以设置背景色。问题是我想把标题放在工具栏的中心,然后我建议操作栏的其他地方,请在布局中使用工具栏。我会尝试使用工具栏
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarM);
setSupportActionBar(toolbar);