【发布时间】:2012-01-31 12:00:03
【问题描述】:
这是我的 main.xml 代码
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/tabs" />
<ScrollView
android:fillViewport="true"
android:scrollbars="@null"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:paddingTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- first text_view -->
<TextView
android:background="@color/grey"
android:textColor="@color/white"
android:text="@string/category"
android:id="@+id/category1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="65dp"
android:textSize="17dp"
android:typeface="serif"/>
<!-- first horizontal_scrollview -->
<HorizontalScrollView
android:scrollbars="@null"
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/linearLayout1"
android:orientation="horizontal"
android:visibility="visible"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<!-- image_view should be here -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</merge>
这是我的 tabs.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#333333">
<TextView
android:textColor="@color/gradient_green"
android:id="@+id/viewall"
android:layout_width="85dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="17dp"
android:textStyle="bold"
android:text="@string/view_all"
android:onClick="onClick"
android:focusable="false"
android:clickable="true" />
<TextView
android:textColor="@color/white"
android:id="@+id/pic"
android:layout_width="45dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/viewall"
android:textSize="17dp"
android:textStyle="bold"
android:text="@string/pic"
android:onClick="onClick"
android:focusable="false"
android:clickable="true" />
</RelativeLayout>
这是 Main.java 中的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView all = (TextView) this.findViewById(R.id.viewall);
TextView pic = (TextView) this.findViewById(R.id.pic);
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView all = (TextView) findViewById(R.id.viewall);
TextView pic = (TextView) findViewById(R.id.pic);
Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
pic.setTextColor(getResources().getColorStateList(R.color.white));
}
});
pdf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView all = (TextView) findViewById(R.id.viewall);
TextView pic = (TextView) findViewById(R.id.pic);
Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
all.setTextColor(getResources().getColorStateList(R.color.white));
pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
}
});
}
所以,如果我将 Main.class 或 Main.java 中的 setContentView() 设置为 setContentView(R.layout.tabs) 而不是 setContentView(R.layout.main),onClick() 有效,我应该怎么做阻止 onClick() 不起作用的代码有什么问题或有什么问题?
【问题讨论】:
-
我认为这两个语句应该在 oncreate 或 onstart 方法中的其他地方 all = (TextView) this.findViewById(R.id.viewall); pdf = (TextView) this.findViewById(R.id.pic);像所有 = (TextView)findViewById(R.id.viewall); pdf = (TextView)findViewById(R.id.pic);
-
我正在尝试显示像 Pulse News 应用这样的标签,并提出了这个想法,但我无法让它发挥作用
-
我现在可以使用了,感谢您的回答,我坚持使用我的第一个邮政编码...
标签: android android-layout onclick textview