无法访问自定义 ActionBar 的 TextView
Unable to access TextView of the the Custom ActionBar
我正在使用不同的布局并显示在操作栏中。我想以编程方式更改我在布局中使用的 TextView 的值。
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashscreen_background">
<TextView
android:id="@android:id/text1"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="M:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="@android:id/text2"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="N:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
这里是 java 文件代码:
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.test, null);
TextView tv = (TextView)customView.findViewById(R.id.text1);
ab.setCustomView(customView);
ab.setDisplayShowCustomEnabled(true);
任何人都可以建议我如何以编程方式更改 TextView 的值。
在 xml 中,将 android:id="@android:id/text1"
替换为 android:id="@+id/text1"
。 id 为 text2 ..
的 TextView 也是如此
并且在 java 中,您将能够像这样以编程方式设置它:
TextView tv = (TextView)customView.findViewById(R.id.text1);
tv.setText("Your Text");
要找出@android:id 和@+id 之间的区别,请看这里:
我正在使用不同的布局并显示在操作栏中。我想以编程方式更改我在布局中使用的 TextView 的值。
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashscreen_background">
<TextView
android:id="@android:id/text1"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="M:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="@android:id/text2"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="N:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
这里是 java 文件代码:
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.test, null);
TextView tv = (TextView)customView.findViewById(R.id.text1);
ab.setCustomView(customView);
ab.setDisplayShowCustomEnabled(true);
任何人都可以建议我如何以编程方式更改 TextView 的值。
在 xml 中,将 android:id="@android:id/text1"
替换为 android:id="@+id/text1"
。 id 为 text2 ..
并且在 java 中,您将能够像这样以编程方式设置它:
TextView tv = (TextView)customView.findViewById(R.id.text1);
tv.setText("Your Text");
要找出@android:id 和@+id 之间的区别,请看这里: