Android에서 사용자 지정 제목 표시 줄 텍스트 뷰 값을 동적으로 설정하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/2416844

문제

친구들,

코드와 함께 다음 titlebar.xml 파일을 사용하여 사용자 정의 제목 표시 줄을 만들었습니다.

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  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"
  android:layout_marginLeft="25px"
   android:paddingTop="3px" 
   /> 

각 활동에 맞춤 제목 표시 줄을 표시하는 Java 코드.

@Override
    public void onCreate(Bundle savedInstanceState)
    {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


}

이제 각 활동에서 TextView 값을 동적으로 설정하고 싶습니다. 어떤 사람 도이를 어떻게 달성 할 수 있습니까?

여기서 FindViewById를 사용하여 주 레이아웃에는 해당 이름이 있지만 MyTitle이있는 텍스트 상자가 포함되어 있지 않기 때문에 해당 TextView를 참조하지 않습니다.

모든 도움이 필요합니다.

도움이 되었습니까?

해결책

이것은 사용자 정의 제목을 설정하는 방법입니다.

@Override
protected 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.mytitle);
    }

    final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
    if ( myTitleText != null ) {
        myTitleText.setText("========= NEW TITLE ==========");
        myTitleText.setBackgroundColor(Color.GREEN);
    }


}

다른 팁

SDK 2.1+

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("========= NEW TITLE ==========");
}

당신은 시도 했습니까? setTitle() 당신의 방법 Activity?

my_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/header"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:background="#d4e9a9">

    <ImageView android:src="@drawable/jetpack"
        android:layout_width="wrap_content" android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" android:id="@+id/back"
        android:layout_height="wrap_content" android:layout_alignParentTop="true" />

    <TextView android:id="@+id/title" android:layout_width="wrap_content"
        android:gravity="center_vertical" android:textSize="20px"
        android:textColor="#ffffff" android:layout_alignParentRight="true"
        android:text="New Title" android:background="#a5c639"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>

암호:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);      

((TextView)findViewById(R.id.title)).setText("gradient shadow");

findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ((TextView)findViewById(R.id.title)).setText("loce");
    }
});

사용자 정의 제목 기본값이 고정되어 있으므로 테마를 작성해야합니다.

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">

다음을 시도하십시오.

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     setContentView(R.layout.main);
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
     TextView mytitletext = (TextView) findViewById(R.id.myTitle);
     mytitletext.setText("========= NEW TITLE ==========");
}

당신은 사용할 수 있습니다 hierarchyviewer 귀하의보기에 액세스 할 수 있는지 확인하려면 (계층 구조 그래프에 ID가 표시됩니다)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top