작업 표시 줄의 텍스트를 변경하는 방법
현재 응용 프로그램의 이름 만 표시하고 사용자 정의한 것을 표시하고 내 응용 프로그램의 각 화면마다 다르게 표시하고 싶습니다.
예를 들어, 내 홈 화면은 작업 표시 줄에 'page1'이라고 말하고 앱이 전환하는 다른 활동은 해당 화면 작업 표시 줄에 'page2'를 가질 수 있습니다.
업데이트 : 최신 ActionBar (제목) 패턴 :
참고로, ActionBar 는 API 레벨 11에서 도입되었습니다. ActionBar는 활동 제목 , 탐색 모드 및 검색과 같은 기타 대화식 항목을 표시 할 수있는 활동 상단의 창 기능 입니다.
제목 표시 줄을 사용자 정의하고 응용 프로그램을 통해 일관성있게 만드는 것에 대해 정확하게 기억합니다. 따라서 이전과 비교할 수 있으며 ActionBar 사용의 장점을 나열 할 수 있습니다.
- 시스템이 다른 화면 구성에 적합하게 적용되는 응용 프로그램에서 친숙한 인터페이스를 제공합니다.
- ActionBar는 이미 최상위 추상화가 준비되어 있으므로 개발자는 활동 제목, 아이콘 및 탐색 모드를 표시하기 위해 많은 코드를 작성할 필요가 없습니다.
예를 들면 다음과 같습니다.
=> 정상적인 방법,
getActionBar().setTitle("Hello world App");
getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions
=> 작업 표시 줄 사용자 정의,
예를 들면 다음과 같습니다.
@Override
public void setActionBar(String heading) {
// TODO Auto-generated method stub
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
actionBar.setTitle(heading);
actionBar.show();
}
작업 표시 줄 스타일링 :
ActionBar는 기본적이고 친숙한 모양, 탐색 모드 및 기타 빠른 작업을 제공합니다. 그러나 이것이 모든 앱에서 동일하게 보이는 것은 아닙니다. UI 및 디자인 요구 사항에 따라 사용자 지정할 수 있습니다. 스타일과 테마를 정의하고 작성하면됩니다.
더 읽기 : 액션 바 스타일링
ActionBar의 스타일을 생성하려면이 스타일 생성기 도구를 사용하면 도움이됩니다.
===================================================== ================================
이전 : 이전 :
=> 정상적인 방법,
각 화면의 제목 (예 : 활동)을 변경할 수 있습니다. Android:label
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
=> 사용자 정의-제목-막대
그러나 원하는 방식으로 제목 표시 줄을 사용자 정의 Want to put Image icon and custom-text
하려면 다음 코드를 사용하십시오.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/icon1"/>
<TextView
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
</LinearLayout>
TitleBar.java
public class TitleBar extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if (myTitleText != null) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then
// "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
strings.xml
strings.xml 파일은 values
폴더 아래에 정의되어 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Set_Text_TitleBar!</string>
<string name="app_name">Set_Text_TitleBar</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
매니페스트 파일에서 각 활동에 대한 레이블을 정의 할 수 있습니다.
활동의 일반적인 정의는 다음과 같습니다.
<activity
android:name=".ui.myactivity"
android:label="@string/Title Text" />
제목 텍스트는이 활동에 대한 문자열 자원의 ID로 대체되어야합니다.
제목 텍스트를 동적으로 설정하려는 경우 코드에서 제목 텍스트를 설정할 수도 있습니다.
setTitle(address.getCity());
이 줄로 제목은 내 활동의 oncreate 메소드에서 특정 주소의 도시로 설정됩니다.
당신은 프로그래밍 방식으로 사용하여 제목을 정의 할 수 있습니다 setTitle
당신 내에서 Activity
,이 방법 중 하나 받아 들일 수 String
귀하의 정의 또는 ID values/strings.xml
파일을. 예:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}
다음 두 가지 방법 중 하나로 ActionBar 제목을 변경할 수 있습니다.
매니페스트 :의 매니페스트 파일에서 각 활동의 레이블을 설정하십시오.
android:label="@string/TitleWhichYouWantToDisplay"
코드에서 : 코드에서, String 또는 id를 String으로하여 setTitle () 메소드를 호출하십시오.
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { setTitle(R.string.TitleWhichYouWantToDisplay); // OR You can also use the line below // setTitle("MyTitle") setContentView(R.layout.activity_main); } }
Activity.onCreate () 콜백 내부 또는 제목을 변경해야하는 다른 장소 :
getSupportActionBar().setTitle("Whatever title");
이것을 시도하십시오 ...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setTitle(String.format(your_format_string, your_personal_text_to_display));
setContentView(R.layout.your_layout);
...
...
}
그것은 나를 위해 작동
조금 오래되었지만 같은 문제가있었습니다. 나는 이렇게했다 :
strings.xml
<string name="title_awesome_app">My Awesome App</string>
AndroidManifest.xml 에서 설정했는지 확인하십시오 .
<activity
...
android:label="@string/title_awesome_app" >
...
</activity>
쉬우 며 null 참조 및 기타 사항에 대해 걱정할 필요가 없습니다.
getActionBar().setTitle("edit your text");
작업 표시 줄 이름을 변경하는 가장 쉬운 방법은 AndroidManifest.xml로 이동하여이 코드를 입력하는 것입니다.
<activity android:name=".MainActivity"
android:label="Your Label> </activity>
onCreate
메소드 에이 코드를 추가하기 만하면 됩니다.
setTitle("new title");
ActionBar ab = getActionBar();
TextView tv = new TextView(getApplicationContext());
LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // Width of TextView
LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setTextColor(Color.RED);
ab.setCustomView(tv);
자세한 내용은이 링크를 확인하십시오.
http://android--code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html
작업 표시 줄 이름을 변경하는 가장 좋은 방법은 AndroidManifest.xml로 이동하여이 코드를 입력하는 것입니다.
<activity android:name=".YourActivity"
android:label="NameYouWantToDisplay> </activity>
가장 쉬운 방법은 this.setTitle("...")
활동중인 경우 전화 하는 것입니다. 그리고 조각에 있다면 전화하십시오 getActivity().setTitle("...")
.
이 방법을 사용하면 언제든지 제목을 변경할 수 있으므로 이전에 전화 할 필요가 없습니다 setContentView(R.layout.activity_test)
.
AndroidX 및 내비게이션 아키텍처 구성 요소를 사용하는 향후 개발자를위한 것입니다 .
위의 솔루션 중 하나를 사용하여 툴바 제목을 설정하는 대신 백 스택 변경시 동적으로 설정하려는 경우 매우 고통 스러울 수 있으며 다음과 같이 탐색 그래프 에서 조각 의 제목 에 대한 자리 표시자를 설정할 수 있습니다 .
<fragment
android:id="@+id/some_fragment"
android:name="package.SomeFragment"
android:label="Hello {placeholder}"
tools:layout="@layout/fragment_some">
<argument
android:name="placeholder"
app:argType="string" />
</fragment>
자리 표시 자 값은 FragmentDirections
(작업 방법을 통해)을 사용하여 제공해야합니다 .
그런 다음 제목으로 바뀌고 Hello World
(when placeholder = "World"
) 과 같이 표시 됩니다 .
getSupportActionBar().setTitle("title");
참고 URL : https://stackoverflow.com/questions/3438276/how-to-change-the-text-on-the-action-bar
'Programing' 카테고리의 다른 글
Oracle SQL Developer 다중 테이블보기 (0) | 2020.04.18 |
---|---|
Hibernate 및 MySQL을 사용한 생성 타임 스탬프 및 마지막 업데이트 타임 스탬프 (0) | 2020.04.18 |
moment.js로 날짜 형식 지정 (0) | 2020.04.18 |
PyPlot의 역 Y 축 (0) | 2020.04.18 |
자바 : 유닉스 타임 스탬프 날짜 (0) | 2020.04.18 |