How can I get my Layout file to show its label as others do?

大兔子大兔子 提交于 2019-12-25 02:53:11

问题


In most cases, adding an Activity creates a corresponding Layout (xml) file that shows "[ActivityName]" in the Action Bar (I don't know if that's the correct terminology) of the Layout.

However, in one case, that is not so.

With AndroidManifest.xml like this:

<activity
    android:name="hhs.app.VerifyCodeActivity"
    android:label="@string/title_activity_verify_code" >
</activity>
<activity
    android:name="hhs.app.DeliveryItemActivity"
    android:label="@string/title_activity_delivery_item" >
</activity>

...the verbiage on the Action Bar of the Layout for VerifyCodeActivity is the appname, NOT the Activity name (the Activity name displays for the DeliveryItemActivity's corresponding Layout file).

The VerifyCodeActivity Layout should say "Verify Code" because this is in the \values\strings.xml file:

<string name="title_activity_verify_code">Verify Code</string>

Grasping at straws (anything different from other code), I changed this in AndroidManifest.xml:

android:name=".VerifyCodeActivity"

...to this:

android:name="hhs.app.VerifyCodeActivity"

...but it makes no difference. What's going on/how can I solve this?

UPDATE

Here's what I'm talking about -- one Activity shows the caption I would like it to:

Others do not, such as this one:

I can't see why the second one doesn't work (it shows the app name ("HHS") instead of the Activity-specific string). Again, the labels are both set in AndroidManifest.xml, as shown above...???

UPDATE 2

Okay, it's obvious now: AndroidManifest.xml sets the Activity's label to a specific string (title_activity_settings), which is in strings.xml:

<string name="title_activity_settings">HHS Settings</string>

...but that has no effect on the Activity's layout file at design time, because the Layout file has no connection to the Activity at that point. It is only after the Activity is invoked that the connection is made:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
}

The string is applied to the Activity's Action Bar then, at run time (or "emulate time," in my case).


回答1:


This is "as designed" and completely logical.

AndroidManifest.xml sets the Activity's label to a specific string (title_activity_settings), which is in strings.xml:

<string name="title_activity_settings">HHS Settings</string>

...but that has no effect on the Activity's layout file at design time, because the Layout file has no connection to the Activity at that point. It is only after the Activity is invoked that the connection is made:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
}

Then and only then is the string set in AndroidManifest.xml applied to the Action Bar of the Activity.

The reason why the string displays also at design time in one case is because of this in the Layout file:

tools:context="hhs.app.DeliveryItemActivity"

In context:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hhs.app.DeliveryItemActivity">

    <TableRow
    . . .


来源:https://stackoverflow.com/questions/23744235/how-can-i-get-my-layout-file-to-show-its-label-as-others-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!