Activity继承AppCompatActivity导致的TitleBar无法去除

本文记录了解决Android项目中无法移除TitleBar的问题。通常的方法如使用requestWindowFeature或修改manifest文件均无效。最终解决方案为让Activity继承自Activity而非AppCompatActivity。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写这篇博客目的是为了记录自己在学习过程当中填过的坑,如果能够给正在填坑的同学一点启示,荣幸之至。

当在AndroidStudio使用默认的Actvity,都是继承于AppCompatActivity。在做项目的过程当中,发现使用常用的三种去除TitleBar的三种办法都是无法去除继承自AppCompatActivity的Activity的TitleBar的。

这三种方法包括

1:Activity代码中,在setContentView()之前,调用requestWindowFeature(Window.FEATURE_NO_TITLE);

2:manifest.xml文件中<activity>标签中添加 android:theme="@android:style/Theme.NoTitleBar"   (执行过程当中是会报错的,直接导致,APP挂了,主要原因是activity继承自AppCompatActivity)
3:<application>标签加入android:theme="@android:style/Theme.NoTitleBar" 


解决方法:

处理次问题的方法很简单,就是让活动继承自Activity。




现在是在datepicker_layout.xml代码完全一致的前提下,在代码WeatherInfoActivity和FanHui代码中使用的datepicker_layout时间选择器显示的布局样式还是完全不一样,给我直接一次性解决布局样式完全不一样的问题。WeatherInfoActivity:package com.jd.projects.wlw.weatherinfo; import static com.jd.projects.wlw.weatherinfo.AllInfoMap.KEY_SITE_INFO; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import com.jd.projects.wlw.DeviceMapActivity; import com.jd.projects.wlw.R; import com.jd.projects.wlw.bean.MapDataBean; import com.jd.projects.wlw.bean.new_webservice.MapDataBeanNew; import com.jd.projects.wlw.fragment.CureDataFragment; import com.jd.projects.wlw.fragment.HistoryDataFragment; import com.jd.projects.wlw.fragment.MonthDataFragment; import com.jd.projects.wlw.fragment.RealTimeFragment; import com.jd.projects.wlw.fragment.TjfxDataFragment; import com.jd.projects.wlw.update.Utils; import java.text.SimpleDateFormat; import java.util.Calendar; public class WeatherInfoActivity extends FragmentActivity implements OnClickListener { private LinearLayout layout_tqyb, layout_nyqx, layout_nyzx, layout_gdnq, layout_tjfx,layout_location; private ImageView image_ntqx, image_tqyb, image_nyzx, image_gdnq, image_tjfx; private Fragment mContent; public static MapDataBean realdata; // public static String asitename; private Calendar calendar = Calendar.getInstance(); private Context context; private ArrayAdapter<String> spinneradapter; private static final String[] m2 = {"空气温度", "空气湿度", "土壤温度1", "土壤温度2", "土壤温度3", "土壤湿度", "光照度", "蒸发量", "降雨量", "风速", "风向", "结露", "气压", "总辐射", "光合有效辐射"}; private static final String[] m1 = {"空气温度", "空气湿度", "土壤温度1", "土壤温度2", "土壤温度3", "土壤湿度1", "土壤湿度2", "土壤湿度3"}; private String spinnervaluse02; TextView time1; private String nsitetype; private MapDataBeanNew curSiteInfo; private double intentLat = 0.0; private double intentLng = 0.0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wheatherinfo); try { intentLat = getIntent().getDoubleExtra("intentLat", 0.0); intentLng = getIntent().getDoubleExtra("intentLng", 0.0); context = this; //getData(); curSiteInfo = (MapDataBeanNew) getIntent().getSerializableExtra(KEY_SITE_INFO); initView(); if(intentLat == 0 || intentLng == 0){ layout_location.setVisibility(View.GONE); }else{ layout_location.setVisibility(View.VISIBLE); } } catch (Exception e){ Log.d("mcg",e.getMessage()); e.printStackTrace(); } } private void getData() { SharedPreferences preferences = getSharedPreferences("wlw_settings", MODE_PRIVATE); String neiip = preferences.getString("neiip", ""); String mark = preferences.getString("netmode", "");//内网访问还是外网访问? nsitetype = AllInfoMap.nsitetype;// } private void initView() { nsitetype = AllInfoMap.nsitetype;// layout_tqyb = (LinearLayout) findViewById(R.id.tab1); layout_nyqx = (LinearLayout) findViewById(R.id.tab2); layout_nyzx = (LinearLayout) findViewById(R.id.tab3); layout_gdnq = (LinearLayout) findViewById(R.id.tab4); layout_tjfx = (LinearLayout) findViewById(R.id.tab5); layout_location = (LinearLayout) findViewById(R.id.tab6); image_ntqx = (ImageView) findViewById(R.id.image_qixiang);//2 image_tqyb = (ImageView) findViewById(R.id.image_yubao);//1 image_nyzx = (ImageView) findViewById(R.id.image_zixun);//3 image_gdnq = (ImageView) findViewById(R.id.image_gdnq);//4 image_tjfx = (ImageView) findViewById(R.id.image_tjfx);//5 layout_tqyb.setOnClickListener(this); layout_nyqx.setOnClickListener(this); layout_nyzx.setOnClickListener(this); layout_gdnq.setOnClickListener(this); layout_tjfx.setOnClickListener(this); layout_location.setOnClickListener(this); mContent = RealTimeFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); } @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { switch (v.getId()) { case R.id.tab1: layout_tqyb.setBackgroundResource(R.drawable.tabshape_bg); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_nyzx.setImageResource(R.drawable.curve); image_ntqx.setImageResource(R.drawable.history_pic); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic_on); mContent = RealTimeFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab2: layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.drawable.tabshape_bg); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic_on); image_nyzx.setImageResource(R.drawable.curve); mContent = HistoryDataFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab3: showExitGameAlert(); break; case R.id.tab4: layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.drawable.tabshape_bg); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic_on); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve); mContent = CureDataFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab5: jinDuJiaozhang(); break; case R.id.tab6: Intent intent = new Intent(WeatherInfoActivity.this, DeviceMapActivity.class); intent.putExtra("intentLat",intentLat); intent.putExtra("intentLng",intentLng); startActivity(intent); break; case R.id.dateselect1: //弹窗日期选择 new MonPickerDialog(context, dateListener1, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show(); break; } } private DatePickerDialog.OnDateSetListener dateListener1 = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { calendar.set(Calendar.YEAR, arg1);// 将给定的日历字段设置为给定值。 //calendar.set(Calendar.MONTH, arg2); //calendar.set(Calendar.DAY_OF_MONTH, arg3); SimpleDateFormat df = new SimpleDateFormat("yyyy"); time1.setText(df.format(calendar.getTime())); } }; /** * 时间选择器 */ @SuppressLint("SimpleDateFormat") private void showExitGameAlert() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); // *** 主要就是在这里实现这种效果的. // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容 window.setContentView(R.layout.datepicker_layout); // 为确认按钮添加事件,执行退出应用操作 DatePicker dp = (DatePicker) window.findViewById(R.id.dpPicker); final Calendar calendar = Calendar.getInstance(); // final SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月"); final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); // 隐藏日期View ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); dp.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), (view, year, monthOfYear, dayOfMonth) -> { // 获取一个日历对象,并初始化为当前选中的时间 calendar.set(year, monthOfYear, dayOfMonth); }); RelativeLayout ok = (RelativeLayout) window.findViewById(R.id.YES); ok.setOnClickListener(v -> { layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.drawable.tabshape_bg); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve_hover); String dataTime = format.format(calendar.getTime()); // 携带数据跳转页面 mContent = new MonthDataFragment(); Bundle bundle = new Bundle(); bundle.putString("datatime", dataTime); bundle.putSerializable(KEY_SITE_INFO, curSiteInfo); mContent.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); dlg.cancel(); }); // 关闭alert对话框架 RelativeLayout cancel = (RelativeLayout) window.findViewById(R.id.NO); cancel.setOnClickListener(v -> dlg.cancel()); } /** * 重写datePicker 1.只显示 年-月 2.title 只显示 年-月 * * @author lmw */ public class MonPickerDialog extends DatePickerDialog { public MonPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); //this.setTitle(year + "年" + (monthOfYear + 1) + "月"); this.setTitle(year + "年"); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE); } @Override public void onDateChanged(DatePicker view, int year, int month, int day) { super.onDateChanged(view, year, month, day); //this.setTitle(year + "年" + (month + 1) + "月"); this.setTitle(year + "年"); } } private void jinDuJiaozhang() { final Dialog myDialog = new Dialog(context); //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); myDialog.show(); // 设置宽度为屏幕的宽度 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes(); lp.width = (int) (display.getWidth()); // 设置宽度 myDialog.getWindow().setAttributes(lp); //myDialog.setCancelable(false);//调用这个方法时,按对话框以外的地方不起作用。按返回键也不起作用 myDialog.setCanceledOnTouchOutside(false);//调用这个方法时,按对话框以外的地方不起作用。按返回键还起作用 Window window = myDialog.getWindow(); window.setContentView(R.layout.dialog_et22);// setContentView()必须放在show()的后面,不然会报错 Spinner sp_01 = (Spinner) window.findViewById(R.id.sp_01); // 将可选内容与ArrayAdapter连接起来 if (nsitetype.equals("01") || !Utils.isOldDevice(curSiteInfo.getId())) { // 十五项因子 spinneradapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, m2); } else if (nsitetype.equals("02")) { // 八项因子 spinneradapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, m1); } // 设置下拉列表的风格 spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 将adapter 添加到spinner中 sp_01.setAdapter(spinneradapter); sp_01.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub /*if (position == 0) { spinnervaluse02 = "卵"; } else if (position == 1) { spinnervaluse02 = "幼虫"; } else if (position == 2) { spinnervaluse02 = "蛹"; } else if (position == 3) { spinnervaluse02 = "成虫"; }*/ if (nsitetype.equals("01") || !Utils.isOldDevice(curSiteInfo.getId())) { // 十五项因子 spinnervaluse02 = m2[position]; } else if (nsitetype.equals("02")) { // 八项因子 spinnervaluse02 = m1[position]; } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); time1 = (TextView) window.findViewById(R.id.time1); LinearLayout dateselect1 = (LinearLayout) window.findViewById(R.id.dateselect1); // 初始化当前时间 updateDate(); dateselect1.setOnClickListener(this); Button btn_ensure = (Button) window.findViewById(R.id.btn_ensure); Button btn_cancel = (Button) window.findViewById(R.id.btn_cancel); btn_ensure.setOnClickListener(v -> { //spinnervaluse02 time1 //统计分析 layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.drawable.tabshape_bg); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_01); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve); mContent =new TjfxDataFragment(); Bundle bundle = new Bundle(); bundle.putString("spinnervaluse", spinnervaluse02); bundle.putString("time1", time1.getText().toString()); bundle.putSerializable(KEY_SITE_INFO,curSiteInfo); mContent.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); myDialog.dismiss(); }); btn_cancel.setOnClickListener(v -> { // TODO Auto-generated method stub myDialog.dismiss(); }); } private void updateDate() {//时间控件 SimpleDateFormat df = new SimpleDateFormat("yyyy"); time1.setText(df.format(calendar.getTime())); } } FanHui:package com.videogo.ui.login; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.videogo.openapi.EZOpenSDK; import com.videogo.widget.TitleBar; import ezviz.ezopensdk.R; import java.util.Calendar; import java.util.Locale; public class FanHui extends AppCompatActivity { private static final String TAG = "EZPreview"; private String mAppKey; private String mDeviceSerial; private String mVerifyCode; private String mAccessToken; private int mCameraNo; private TextView mDateTextView; private int mSelectedYear, mSelectedMonth, mSelectedDay; private static final String KEY_APPKEY = "appkey"; private static final String KEY_SERIAL = "serial"; private static final String KEY_VERIFYCODE = "VerifyCode"; private static final String KEY_ACCESSTOKEN = "accessToken"; private static final String KEY_CAMERANO = "cameraNo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ez_playback_list_page); extractParametersFromIntent(); final Calendar calendar = Calendar.getInstance(); mSelectedYear = calendar.get(Calendar.YEAR); mSelectedMonth = calendar.get(Calendar.MONTH); mSelectedDay = calendar.get(Calendar.DAY_OF_MONTH); // 设置日期显示模块 setupDatePicker(); View fanHui = findViewById(R.id.fanhui); fanHui.setOnClickListener(v -> finish()); Button huifangBtn = findViewById(R.id.fanhui); huifangBtn.setOnClickListener(v -> { Intent intent = new Intent(FanHui.this, MainActivity.class); intent.putExtra("deviceSerial", mDeviceSerial); intent.putExtra("cameraNo", mCameraNo); intent.putExtra("accessToken", mAccessToken); intent.putExtra("appkey", mAppKey); intent.putExtra("verifyCode", mVerifyCode); startActivity(intent); }); } private void setupDatePicker() { mDateTextView = findViewById(R.id.date_text); ImageButton datePickerButton = findViewById(R.id.date_picker_button); updateDateDisplay(); datePickerButton.setOnClickListener(v -> showDatePickerDialog()); } private void updateDateDisplay() { String formattedDate = String.format(Locale.getDefault(), "%d年%02d月%02d日", mSelectedYear, mSelectedMonth + 1, // 月份需要+1 mSelectedDay); mDateTextView.setText(formattedDate); } private void showDatePickerDialog() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.datepicker_layout); // 设置对话框宽度 WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 匹配父容器宽度 window.setAttributes(lp); // 初始化日期选择器 DatePicker dpPicker = window.findViewById(R.id.dpPicker); dpPicker.init(mSelectedYear, mSelectedMonth, mSelectedDay, null); // 获取按钮 RelativeLayout yesButton = window.findViewById(R.id.YES); RelativeLayout noButton = window.findViewById(R.id.NO); // 设置确定按钮点击事件 yesButton.setOnClickListener(v -> { mSelectedYear = dpPicker.getYear(); mSelectedMonth = dpPicker.getMonth(); mSelectedDay = dpPicker.getDayOfMonth(); updateDateDisplay(); dlg.dismiss(); }); // 设置取消按钮点击事件 noButton.setOnClickListener(v -> dlg.dismiss()); } private void extractParametersFromIntent() { Bundle extras = getIntent().getExtras(); if (extras != null) { mAppKey = extras.getString(KEY_APPKEY, ""); mDeviceSerial = extras.getString(KEY_SERIAL, ""); mVerifyCode = extras.getString(KEY_VERIFYCODE, ""); mAccessToken = extras.getString(KEY_ACCESSTOKEN, ""); mCameraNo = extras.getInt(KEY_CAMERANO, 0); Log.d(TAG, "Received parameters:"); Log.d(TAG, "AppKey: " + mAppKey); Log.d(TAG, "DeviceSerial: " + mDeviceSerial); Log.d(TAG, "VerifyCode: " + mVerifyCode); Log.d(TAG, "AccessToken: " + mAccessToken); Log.d(TAG, "CameraNo: " + mCameraNo); } else { Log.e(TAG, "No parameters received from intent"); } } }
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值