如何获取和android中设置字符串值的资源字符串值、资源、android

2023-09-07 10:57:45 作者:君兮寄相思

我已经声明了一个字符串

<string name="Please">Please Select</string>

RES /价值/ strings.xml中。现在我要访问此字符串的价值,但我能得到什么可以去钱柜ID不珍惜这样的:

in the res/values/Strings.xml. Now I want to access this String value but what I can get can go till id not value like:

 int i = findViewById(R.string.Please);

但我要的是字符串只值

感谢您的回答,但我的问题是有点大。

Thanks for all of your answer but my problem is a little bigger

我想要做的只是显示日期与APP_NAME

What I wanted to do is just to display date with app_name

这是你的答案我可以得到APP_NAME字符串但我是否可以设置这与APP_NAME当前日期

from all your answers i can get the app_name String but whether I can set This app_name with current date

<string name="app_name">My app</string>

和还我设置我这样

My app(in Left alignment)             Date (in right aligment)

和还第一次活动开始时我想设置APP_NAME而不是在每一个活动;

and also on start of first activity i want to set app_name and not on every activity;

我觉得它可能工作让我试试

I think it may work let me try

setResource().setString(R.string.Please);

但没有setResource()方法抱歉。

but no setResource() method sorry.

推荐答案

您不能设置一个字符串,其静态的,不能在运行时改变,你想做的事是这样的。

You cant set a string, its static, and cant be changed at run-time, what you want to do is something like this

        Calendar cal = Calendar.getInstance();
        String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
        setTitle(date);

您想设置标题的日期吧?

Your trying to set the title as the date right?

要拥有两个头衔实在是有点困难,首先你需要一个布局,调用此custom_title_1

To have two titles it is a bit harder, first you need a layout, call this custom_title_1

这是XML code为布局

This is the xml code for that layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/custom_title_left" />
    <TextView android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/custom_title_right" />
</RelativeLayout>

然后在你的code调用UI之前,你需要调用这样的:

Then in your code before you call the UI you need to call this:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

然后设置内容视图

then set you content view

然后再调用这个将设置标题:

then call this which will set the title:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

现在,你可以找到两个textViews并设置文本:

Now you can find the two textViews and set the text:

TextView leftText = (TextView) findViewById(R.id.left_text);
TextView rightText = (TextView) findViewById(R.id.right_text);

Calendar cal = Calendar.getInstance();
String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
String app_name = getString(R.string.app_name);
leftText.setText(app_name);
rightText.setText(date);