작업 표시 줄 제목 및 부제 설정
컴파일 시간 전에 액션 바의 제목과 하위 항목을 설정하고 싶습니다. 다음과 같이 할 방법이 있습니다.
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
그러나 나는 그것을 런타임에하고 싶지 않다. 이 제목을 지정할 수있는 xml 파일이나 위치가 있습니까?
나는 이것을 달성하려고 노력하고 있습니다.
편집하다:
xml에서 원하는 이유는 내 앱이 API 레벨 8에서 지원되기를 원하기 때문입니다. getActionBar () 메소드는 최소한 레벨 11에서 지원됩니다.
다음과 같이 두 버전을 모두 코딩 할 수 있습니다.
/**
* Sets the Action Bar for new Android versions.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void actionBarSetup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
}
그런 다음 전화 actionBarSetup()
에서 onCreate()
. 는 if
새 Android 버전에서만 코드를 실행하며 @TargetApi
코드를 컴파일 할 수 있도록합니다. 따라서 이전 및 새 API 버전 모두에 대해 안전합니다.
또는 ActionBarSherlock ( edit 참조 )을 사용 하여 모든 버전에서 ActionBar를 사용할 수도 있습니다. 활동을 확장 SherlockActivity
하고 호출하는 것과 같은 몇 가지 변경을 수행해야 getSupportActionBar()
하지만 매우 좋은 글로벌 솔루션입니다.
편집하다
이 답변이 원래 작성되었을 때 ActionBarSherlock
이후 더 이상 사용되지 않는 은 호환성 솔루션으로 이동했습니다.
오늘날 Google의 appcompat-v7
라이브러리 는 동일한 기능을 제공하지만 Google에서 지원 (및 적극적으로 업데이트)합니다. ActionBar
필수 를 구현하려는 활동 :
- 넓히다
AppCompatActivity
Theme.AppCompat
미분을 사용하다
ActionBar
이 라이브러리를 사용하여 인스턴스 를 가져 오려면 적절한 이름이 지정된 getSupportActionBar()
메서드가 사용됩니다.
액션 바의 제목은 다음과 같은 AndroidManifest에있을 수 있습니다.
<activity
. . .
android:label="string resource"
. . .
</activity>
android : label 활동에 대한 사용자가 읽을 수있는 레이블입니다. 활동이 사용자에게 표시되어야 할 때 레이블이 화면에 표시됩니다. 종종 활동 아이콘과 함께 표시됩니다. 이 속성이 설정되지 않은 경우 애플리케이션 전체에 설정된 레이블이 대신 사용됩니다 (요소의 레이블 속성 참조). 여기에서 설정하든 요소에서 설정하든 활동의 레이블은 모든 활동의 인 텐트 필터에 대한 기본 레이블이기도합니다 (요소의 레이블 속성 참조). 레이블은 사용자 인터페이스의 다른 문자열처럼 지역화 될 수 있도록 문자열 리소스에 대한 참조로 설정되어야합니다. 그러나 응용 프로그램을 개발하는 동안 편의를 위해 원시 문자열로 설정할 수도 있습니다.
이걸로 해봐:
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle("This is Title");
ab.setSubtitle("This is Subtitle");
There is another option that no one commented, from the Support Library V7 was implemented by Google ActionBarSupport
, which is compatible ActionBar
from Android 2.2 to the last.
If you change your ActionBar
this you can do getSupportActionBar().setTitle("")
on all Android versions.
Nope! You have to do it at runtime.
If you want to make your app backwards compatible, then simply check the device API level. If it is higher than 11, then you can fiddle with the ActionBar and set the subtitle.
if(Integer.valueOf(android.os.Build.VERSION.SDK) >= 11){
//set actionbar title
}
<activity android:name=".yourActivity" android:label="@string/yourText" />
Put this code into your android manifest file and it should set the title of the action bar to what ever you want!
Try This:
In strings.xml add your title and subtitle...
ActionBar ab = getActionBar();
ab.setTitle(getResources().getString(R.string.myTitle));
ab.setSubtitle(getResources().getString(R.string.mySubTitle));
Try This
Just go to your Manifest file. and You have define the label for each activity in your manifest file.
<activity
android:name=".Search_Video"
android:label="@string/app_name"
android:screenOrientation="portrait">
</activity>
here change
android:label="@string/your_title"
This is trivial one-liner in Kotlin
supportActionBar?.title = getString(R.string.coolTitle)
supportActionBar?.title = "Hola tio"
supportActionBar?.subtitle = "Vamos colega!"
활동의 경우이 접근 방식을 사용하여 매니페스트에서 제목과 함께 부제를 지정할 수 있습니다.
명백한:
<activity
android:name=".MyActivity"
android:label="@string/my_title"
android:description="@string/my_subtitle">
</activity>
활동:
try {
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
//String title = activityInfo.loadLabel(getPackageManager()).toString();
int descriptionResId = activityInfo.descriptionRes;
if (descriptionResId != 0) {
toolbar.setSubtitle(Utilities.fromHtml(getString(descriptionResId)));
}
}
catch(Exception e) {
Log.e(LOG_TAG, "Could not get description/subtitle from manifest", e);
}
이렇게하면 제목 문자열을 한 번만 지정하면 바로 옆에 부제목을 지정할 수 있습니다.
을 사용하여 작업 표시 줄에서 제목을 설정할 수 있습니다 AndroidManifest.xml
. 라벨 추가activity
<activity
android:name=".YourActivity"
android:label="Your Title" />
참고 URL : https://stackoverflow.com/questions/14297178/setting-action-bar-title-and-subtitle
'Nice programing' 카테고리의 다른 글
Javascript를 편집하는 동안 Eclipse 오류가 지속적으로 나타납니다. (0) | 2020.12.07 |
---|---|
Void와 매개 변수 없음의 차이점은 무엇입니까? (0) | 2020.12.07 |
Angular $ location.path가 작동하지 않습니다. (0) | 2020.12.07 |
Excel VBA에서 구현을 사용하는 방법 (0) | 2020.12.07 |
Amazon S3 리디렉션 및 Cloudfront (0) | 2020.12.07 |