【问题标题】:Android: Buttons in Footer not clickable with map fragmentAndroid:页脚中的按钮无法使用地图片段点击
【发布时间】:2013-11-02 04:02:49
【问题描述】:

我的地图布局中包含页脚布局。页脚有 3 个 ImageButtons。我想尽一切办法找出点击次数,但没有成功。我只需要页脚在此活动中工作,因此如果可能,我认为没有必要创建额外的类文件。

footer.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:weightSum="4"
    android:clickable="true"
    android:background="@drawable/footer">

    <ImageButton
        android:id="@+id/guardianButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        android:layout_weight="1"
        android:background="@drawable/guardianbutton" >

</ImageButton>

    <ImageButton
        android:id="@+id/sosButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:background="@drawable/sosbutton"
        android:paddingLeft="5dp"
        android:layout_weight="2"
        android:paddingRight="5dp" />

    <ImageButton
        android:id="@+id/settingsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sosButton"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:background="@drawable/settingsbutton" >

 </ImageButton>
  </LinearLayout>
  </merge>

ma​​pviewmain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapViewMain" 

 <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@layout/header"
    class="com.google.android.gms.maps.SupportMapFragment" />

    <!--  Header Starts-->
 <LinearLayout 
android:orientation="vertical"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header"
android:paddingTop="5dip"
android:paddingBottom="5dip">

 </LinearLayout>
 <!--  Header Ends -->    

        <!-- Footer Start -->

    <RelativeLayout android:id="@+id/RelativeLayout03"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    <include layout="@layout/footer" /></RelativeLayout>
    <!-- Footer Ends -->

 </RelativeLayout> 

一切都显示正确。我只是无法访问 ImageButtons 的 onclick 侦听器。到目前为止,我尝试了以下方法:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.footer, container, false);
    container.addView(view);



ImageButton loadmore = (ImageButton) view.findViewById(R.id.sosButton);
loadmore.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Toast.makeText(MapViewMain.this, "SOS Button pressed", Toast.LENGTH_LONG).show();
    }
});

    return view;
}

【问题讨论】:

    标签: android google-maps footer imagebutton onclicklistener


    【解决方案1】:

    因为您的片段不包含图像按钮。您的 mapviewmain.xml 在您的活动下,因此您必须这样做。

    class MainFragmentActiviy extend FragmentActivity
     {  public ImageButton loadmore;
    
      onCreate(Bundle savedInstanceState)
        {
          setContentView(R.layout.mapvuewmain.xml);
           loadmore=findViewById(R.id.sosButton);
        }
    }
    

    在你的片段中

    public void OnCreate(Bundle SavedInstanceState)
    {
       MainFragmentActiviy yourActivity=getActivity();
    }
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.footer, container, false);
    yourActivity.loadmore.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Toast.makeText(MapViewMain.this, "SOS Button pressed", Toast.LENGTH_LONG).show();
    }
     });
     return view;
    }
    

    【讨论】:

    • 感谢您的回答哈迪克。我仍然无法绕过它。目前我有一个活动 MapViewMain.java 使用 mapviewmain.xml 布局,其中包括 footer.xml 布局。你的意思是我将不得不创建一个单独的活动,例如footer.java,它将包含你上面的片段代码?您的 MainFragmentActivity 将是我的 MapViewMain.java?此外,我在使用此代码时遇到问题: MainFragmentActiviy yourActivity=getActivity();我的页脚类是否必须扩展片段?获取更改为 FragmentActivity 而不是 MainFragmentActivity 的建议
    • 不,您不需要创建单独的活动。只是您的活动必须扩展 FragmentActivity(例如 MapViewMain 扩展 FragmentActivity)。只为您的活动这样做。然后在你的片段中做我的回答。
    • 我真的跟不上你了。请查看我编辑的答案我到目前为止尝试过的内容。我认为您的片段代码有一些错误。您没有在布局中找到按钮,而是设置了 onclicklistener...
    • 用这个来解决我的问题:stackoverflow.com/questions/4121986/…
    • @SunnySonic 是的,我在 MainFragmentActivity 上找到 Imagebutton "loadmore" 看到我的答案,你可以像我在 answer.thanks 中所做的那样获得 fragmentActivity 对你的片段和 setOnclicklistener 的引用。
    猜你喜欢
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 2011-07-14
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多