i have developed an app , which uses the settings using Preference Activity and Action Title Bar . When i press the three dots-> settings button , then it displays not responding , can u help .
清单文件代码是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demoproject.swatygupta.com.demoproject_actionbar">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity2_2PreferenceActivityScreen" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
菜单文件夹中的xml文件是
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:DemoProject_ActionBar="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="demoproject.swatygupta.com.demoproject_actionbar.MainActivity">
<item
android:id="@+id/action_settings"
android:icon="@android:drawable/ic_menu_manage"
android:title="@string/action_settings"
DemoProject_ActionBar:showAsAction="never" />
我开发了一个应用程序,它使用首选项活动和动作标题栏来设置。当我按下三个点 - &gt;设置按钮,然后它显示没有响应,你可以帮忙。
主要活动类请在下面找到,请在调用方法时查找是否存在任何错误.-&gt;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, Activity2_2PreferenceActivityScreen.class));
return true;
}
return super.onOptionsItemSelected(item);}
}
首选项屏幕活动代码 - &gt;
public final class Activity2_2PreferenceActivityScreen extends PreferenceActivity {
public Activity2_2PreferenceActivityScreen()
{
super();
} // constructor()
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Camera preference
final ListPreference PreferenceofCamera = (ListPreference) findPreference("camera");
setCameraPreferences(PreferenceofCamera);
PreferenceofCamera.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
setCameraPreferences(PreferenceofCamera);
return false;
}
});
// JPEG size preference
final ListPreference PreferenceofSize = (ListPreference) findPreference("size");
setSizePreferences(PreferenceofSize, PreferenceofCamera);
PreferenceofSize.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
setSizePreferences(PreferenceofSize, PreferenceofCamera);
return false;
}
});
} // onCreate(Bundle)
private void setCameraPreferences(final ListPreference cameraPreference)
{
final int numberOfCameras = Camera.getNumberOfCameras();
final CharSequence[] entries = new CharSequence[numberOfCameras];
final CharSequence[] entryValues = new CharSequence[numberOfCameras];
final Camera.CameraInfo Infoabtcamera = new Camera.CameraInfo();
for (int cameraIndex = 0; cameraIndex < numberOfCameras; cameraIndex++)
{
Camera.getCameraInfo(cameraIndex, Infoabtcamera);
String cameraFacing;
switch (Infoabtcamera.facing)
{
case Camera.CameraInfo.CAMERA_FACING_BACK:
cameraFacing = "Back";
break;
case Camera.CameraInfo.CAMERA_FACING_FRONT:
cameraFacing = "Front";
break;
default:
cameraFacing = "unknown";
} // switch
entries[cameraIndex] = "Camera " + cameraIndex + " " + cameraFacing;
entryValues[cameraIndex] = String.valueOf(cameraIndex);
} //for
cameraPreference.setEntries(entries);
cameraPreference.setEntryValues(entryValues);
} // setCameraPreferences(ListPreference)
private void setSizePreferences(final ListPreference sizePreference,
final ListPreference cameraPreference)
{
final String cameraPreferenceValue = cameraPreference.getValue();
final int cameraIndex;
if (cameraPreferenceValue != null)
{
cameraIndex = Integer.parseInt(cameraPreferenceValue);
} // if
else
{
cameraIndex = 0;
} // else
final Camera camera = Camera.open(cameraIndex);
final Camera.Parameters params = camera.getParameters();
camera.release();
final List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
CharSequence[] entries = new CharSequence[supportedPreviewSizes.size()];
CharSequence[] entryValues = new CharSequence[supportedPreviewSizes.size()];
for (int previewSizeIndex = 0; previewSizeIndex < supportedPreviewSizes.size();
previewSizeIndex++)
{
Camera.Size supportedPreviewSize = supportedPreviewSizes.get(previewSizeIndex);
entries[previewSizeIndex] = supportedPreviewSize.width + "x"
+ supportedPreviewSize.height;
entryValues[previewSizeIndex] = String.valueOf(previewSizeIndex);
} // for
sizePreference.setEntries(entries);
sizePreference.setEntryValues(entryValues);
} / setSizePreferenceData(ListPreference)
}`
答案 0 :(得分:0)
您可以随时使用工具栏,如下所示
嘿把这段代码放在你的布局文件中。
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
并在您的java文件中,此代码应该在那里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
特别是这两行
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
这将向用户显示右上角的软菜单按钮。