2013-11-25 46 views
0

OnClick不起作用。点击布局后没有任何反应。它似乎是可点击的,因为布局会改变它的颜色,但新的布局不会打开。OnClick不适用于LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/window" 
    android:layout_width="295dp" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@drawable/editborder" 
    android:clickable="true" 
    android:onClick="openBigImage"> 

下面是主要活动更多的代码:

public class MyMapActivity extends FragmentActivity implements LocationListener 
{ 

    private Marker marker; 
    private Hashtable<String, String> markers; 
    private ImageLoader imageLoader; 
    private DisplayImageOptions options; 
    private GoogleMap map; 
    private ListView mainListView ; 
    private ArrayAdapter<String> listAdapter ; 




@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_map); 

    // Look up the AdView as a resource and load a request. 
    //AdView adView = (AdView)this.findViewById(R.id.adView); 
    //adView.loadAd(new AdRequest()); 


    // Getting Google Play availability status 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

    // Showing status 
    if(status!=ConnectionResult.SUCCESS) 
    { // Google Play Services are not available 
     int requestCode = 10; 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
     dialog.show(); 

    } 
    else 
    {// Google Play Services are available 
     // Getting reference to the SupportMapFragment of activity_main.xml 
     SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

     if (savedInstanceState == null) { 
      // First incarnation of this activity. 
      mapFragment.setRetainInstance(true); 
     } 
     else 
     { 
      // Reincarnated activity. The obtained map is the same map instance in the previous 
      // activity life cycle. There is no need to reinitialize it. 
      map = mapFragment.getMap(); 
     } 

     setUpMapIfNeeded(); 
    } 
} 

@Override 
protected void onResume() 
{ 
     super.onResume(); 
     setUpMapIfNeeded(); 
} 


public void openBigImage(View v) 
    { 
     setContentView(R.layout.bigpicture); 
    } 

bigpicture.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/bigpicture" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:orientation="vertical"> 

<fragment 
    android:id="@+id/minimap" 
    android:layout_width="200px" 
    android:layout_height="200px" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

<ImageView 
    android:id="@+id/badge" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:adjustViewBounds="true" /> 
</RelativeLayout> 

调用的setContentView()多次曾在其他情况下,如菜单项 “约” ,“设置”等。 试图使没有setContentView。我已经将新的布局放到main.xml中,并使能见度GONE。 OnClick方法应该将可见性更改为可见,但再次没有任何反应。 当我点击线性布局时,Logcat说“11-25 13:47:28.638:D/GestureDetector(3156):[Surface Touch Event] mSweepDown False,mLRSDCnt:-1 mTouchCnt:2 mFalseSizeCnt:0”

+0

可能重复(http://stackoverflow.com/questions/4018772/calling-setcontentview-multiple-times) – Sajmon

+0

能告诉你一些更你的主要活动代码? – GrIsHu

+0

不确定您是否可以多次调用setContentView()函数。 –

回答

0

找到。它是InfoWindow的一个点击,所以我们应该实现onInfoWindowClick。但首先我们必须添加map.setOnInfoWindowClickListener(this);在主要活动中。主要活动必须实现OnInfoWindowClickListener。 我已经将新的LinearLayout添加到main.xml中,使其不可见。 下面是onInfoWindowClick代码:[调用的setContentView()多次]的

@Override 
public void onInfoWindowClick(Marker marker) { 
    LinearLayout secondLL = (LinearLayout) findViewById(R.id.bigpicture); 
    int visibility = secondLL.getVisibility(); 
    if(visibility == View.GONE) 
    { 
     secondLL.setVisibility(View.VISIBLE); 
    } 

} 
-1

我认为你不能使用onClick属性。 你必须使用setOnClickListener()这样的:

LinearLayout layout = (LinearLayout)findViewById(R.id.window); 
layout .setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     YourActivity.this.setContentView(R.layout.bigpicture); 
    } 
}); 
+0

为什么不呢?它相当于xml! –

+0

此应用程序崩溃后开始 –

+0

你是不对的,对不起。 – Sajmon

0

保罗, 一件事是亲近/>。我是假设你已经按照地图教程link并通过了所有清单权限和线性布局其他需求。你可能有一些理由使用px。检查是否正在创建地图。同时给徽章图像一些高度和背景颜色,看看是否发生了一些事情。

我测试了你的代码没有地图片段,它工作正常。 你能发布错误日志吗?

+0

地图的作品,现在我正在添加功能,正好当您点击custon信息窗口时,必须出现另一个布局。它只是线性布局的代码片段来显示“OnClick”。它稍后会用关闭。 –

+0

没有错误,日志只显示我触摸屏幕并没有任何反应。我现在不想使用setContentView()。 –

+0

点击linearlayout后logcat说'11 -25 13:42:24.633:D/GestureDetector(797):[Surface Touch Event] mSweepDown False,mLRSDCnt:-1 mTouchCnt:2 mFalseSizeCnt:0' –

相关问题