작업 표시 줄에 뒤로 버튼 추가
작업 표시 줄에 뒤로 버튼을 추가하려고했습니다.
내 견해가 다음과 같기를 바랍니다. 
작업 표시 줄 왼쪽에 뒤로 버튼을 추가하고 싶습니다.
이 코드를 추가했습니다
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
하지만 작동하지 않습니다.
이 문제를 어떻게 해결할 수 있습니까?
설정 후 actionBar.setHomeButtonEnabled(true);
다음 코드를 추가하십시오.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
더하다
actionBar.setHomeButtonEnabled(true);
다음을 추가하십시오
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
naXa가 제안한대로 itemId작업 표시 줄에 여러 개의 버튼이있는 경우 올바르게 작동하도록에 체크를 추가했습니다 .
이것은 나를 위해 일했습니다.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
SupportActionBar에서 뒤로 버튼을 사용할 때 onSupportNavigateUp () 메서드가 호출됩니다.
설정 후
actionBar.setHomeButtonEnabled(true);
AndroidManifest.xml에서 부모 활동을 구성해야합니다.
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="@style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
자세한 내용은 여기를 참조하십시오. http://developer.android.com/training/implementing-navigation/ancestral.html
이에 접근하는 방법에는 두 가지가 있습니다.
Option 1: Update the Android Manifest If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar
<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>
Option 2: Change a setting for the ActionBar If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Then, detect the 'back button' press and tell Android to close the currently open Activity.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That should do it!
You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method
Duplicate of Android Sherlock ActionBar Up button
더 간단하고 더 좋음 : API> = 16
Manifest의 각 활동에 대해 "parentActivityName"을 추가하기 만하면됩니다. 뒤로 버튼은 자동으로 부모 활동으로 이동합니다.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
이 버튼을 사용하여 뒤로 버튼을 표시하고 이전 활동으로 이동합니다.
final ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
먼저 이것을 사용하십시오 :
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
그런 다음 버튼 클릭 onOptionsItemSelected방법을 설정합니다.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
다른 사람이 솔루션이 필요한 경우
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
onCreate () 메서드에이 줄을 추가합니다.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
이 메서드를 재정의하십시오.
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
참고 URL : https://stackoverflow.com/questions/12070744/add-back-button-to-action-bar
'Nice programing' 카테고리의 다른 글
| Android에서 color 및 color.darker를 사용하십니까? (0) | 2020.11.25 |
|---|---|
| Entity Framework 코어 : DbContextOptionsBuilder '에'usesqlserver '에 대한 정의가 포함되어 있지 않고 확장 메서드'usesqlserver '가 없습니다. (0) | 2020.11.25 |
| 최근 콘솔 명령을 모두 제거하는 방법 (0) | 2020.11.25 |
| React-native run-ios를 macOS의 터미널 대신 iTerm에서 여는 방법은 무엇입니까? (0) | 2020.11.25 |
| 바운스 각도를 계산하는 방법? (0) | 2020.11.25 |