无法单击按钮
我遇到了一个奇怪的问题,屏幕上显示了一个按钮,但我无法单击。这并不是说 onClick
事件没有触发,它似乎在物理上对点击没有反应,就好像它上面有一个透明层一样。
它所构建的主要 Activity
:
public class MusicList extends Activity implements OnClickListener {
...
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.mymusic);
this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
this.myTabHost.setup();
TabSpec ts = myTabHost.newTabSpec("TAB_TAG_1");
ts.setIndicator("Media Item",getResources().getDrawable(R.drawable.music));
ts.setContent(R.id.media_item_tab);
try {
relativeLayout = (RelativeLayout) myTabHost.findViewById(android.R.id.tabcontent).findViewById(R.id.media_item_tab);
} catch (NullPointerException e) {
Log.e(this.getClass().getSimpleName(),"Layout is not correct",e);
}
Button button = (Button)relativeLayout.findViewById(R.id.play_button);
// button.setClickable(true);
button.setOnClickListener(this);
myTabHost.addTab(ts);
}
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(itemData.get("ITEM_URL"))));
}
}
屏幕上完美显示的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/th_set_menu_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65px">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/media_item_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/media_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#40A5D9"
android:gravity="center"
android:layout_alignParentTop="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Title"
>
</TextView>
<ImageView
android:id="@+id/media_cover"
android:layout_width="fill_parent"
android:layout_height="180px"
android:gravity="center"
android:layout_below="@+id/media_title"
android:src="@drawable/geniocover"
>
</ImageView>
<TextView
android:id="@+id/media_author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#86CCF3"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="@+id/media_cover"
android:text="Author"
>
</TextView>
<TextView
android:id="@+id/media_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#DAFFFF"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="@+id/media_author"
android:text="Album (Year)"
>
</TextView>
<Button
android:id="@+id/play_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="#003983"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center"
android:layout_below="@+id/media_album"
android:clickable="true"
>
</Button>
</RelativeLayout>
<ListView
android:id = "@+id/tablist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</TabHost>
有什么建议吗?
谢谢!
I'm having a weird issue with a button that is being shown on screen, but that I can't click. It is not that the onClick
event is not firing, it seems that physically it does not react to clicks, as if it had a transparent layer on top of it.
The main Activity
where it is built on:
public class MusicList extends Activity implements OnClickListener {
...
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.mymusic);
this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
this.myTabHost.setup();
TabSpec ts = myTabHost.newTabSpec("TAB_TAG_1");
ts.setIndicator("Media Item",getResources().getDrawable(R.drawable.music));
ts.setContent(R.id.media_item_tab);
try {
relativeLayout = (RelativeLayout) myTabHost.findViewById(android.R.id.tabcontent).findViewById(R.id.media_item_tab);
} catch (NullPointerException e) {
Log.e(this.getClass().getSimpleName(),"Layout is not correct",e);
}
Button button = (Button)relativeLayout.findViewById(R.id.play_button);
// button.setClickable(true);
button.setOnClickListener(this);
myTabHost.addTab(ts);
}
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(itemData.get("ITEM_URL"))));
}
}
The layout, which is shown perfect on screen is like this:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/th_set_menu_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65px">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/media_item_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/media_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#40A5D9"
android:gravity="center"
android:layout_alignParentTop="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Title"
>
</TextView>
<ImageView
android:id="@+id/media_cover"
android:layout_width="fill_parent"
android:layout_height="180px"
android:gravity="center"
android:layout_below="@+id/media_title"
android:src="@drawable/geniocover"
>
</ImageView>
<TextView
android:id="@+id/media_author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#86CCF3"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="@+id/media_cover"
android:text="Author"
>
</TextView>
<TextView
android:id="@+id/media_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#DAFFFF"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="@+id/media_author"
android:text="Album (Year)"
>
</TextView>
<Button
android:id="@+id/play_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="#003983"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center"
android:layout_below="@+id/media_album"
android:clickable="true"
>
</Button>
</RelativeLayout>
<ListView
android:id = "@+id/tablist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</TabHost>
Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的
FrameLayout
只打算承载一个子级时,它却有两个子级。ListView
是最后添加的,最终将位于顶部,并且可能会接收您的所有输入。You have a
FrameLayout
with two children when it's only intended to host one. TheListView
was last added, will end up on top and is probably receiving all of your input.我认为问题是由您的选项卡布局的复杂结构引起的。
通常,我使用单独的 Activity 来保存 TabLayout,并将不同的 Activity 作为内容放入 tabhost 中 (
tabSpec.setContent(Intent Intent)
)。实际上这应该可以解决你的问题。
顺便说一句,在可以使用 LinearLayout 的地方,请避免使用
RelativeLayout
。I think the problem is caused by the complex structure of your tablayout.
Usually, I use a separate Activity holding the TabLayout and just put different activities as content into the tabhost (
tabSpec.setContent(Intent intent)
).Actually this should solve your problem.
By the way, avoid using
RelativeLayout
where you could use a LinearLayout.