2013-11-02 29 views
0

我在地图布局中包含了页脚布局。页脚有3个ImageButtons。我尝试了所有的方法来弄清楚点击次数,但没有成功。我只需要脚注在这个活动中工作,所以我没有看到它有必要创建一个额外的类文件,如果可能的话。Android:Footer中的按钮无法与地图片段点击

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> 

mapviewmain.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; 
} 

回答

1

因为您的片段不包含imagebutton。你的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; 
} 
+0

谢谢您的回答Hardik。我仍然无法将头围住它。目前我有一个活动MapViewMain.java使用包含footer.xml布局的mapviewmain.xml布局。你的意思是我将不得不创建一个单独的活动,例如footer.java,其中将有你的片段的代码在上面?你的MainFragmentActivity会是我的MapViewMain.java?此外,我有这个代码的问题:MainFragmentActiviy yourActivity = getActivity();我的页脚类是否需要扩展片段?获取建议更改为FragmentActivity而不是MainFragmentActivity – SunnySonic

+0

不,您不需要创建单独的活动。只是您的活动必须扩展FragmentActivity(例如MapViewMain扩展FragmentActivity)。只是为了你的活动。并在你的片段后作为我的答案。 – Hardik

+0

我真的不能跟着你。请参阅我编辑的答案到目前为止我尝试过的。我认为你的片段代码有一些错误。你没有在布局中找到按钮,但设置onclicklistener ... – SunnySonic