如何以及在哪里必须添加用于滑动菜单的片段类中的按钮?

时间:2014-05-01 18:39:07

标签: android button android-fragmentactivity slidingmenu

我正在阅读并使用一个教程,该教程是关于如何在Android应用程序中创建滑动菜单... 教程的地址是this .. activity-main.xml是这样的:

//activity-main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>
  </android.support.v4.widget.DrawerLayout>

实际上我想在fragment_find_people.xml布局中添加按钮以显示简单的吐司......

//fragment_find_people.xml
<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
    android:id="@+id/btnDelete"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      android:text="enter" />
  </RelativeLayout>

我没有使用片段类和视图所以我很困惑... 我发现我无法在“FindPeopleFragment”类中创建扩展Fragment的Button对象.... 有课是:

public class FindPeopleFragment extends Fragment {
Button btnAdd;
public FindPeopleFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
    return rootView;

}

}

当我写道:

    btnAdd=(Button)this.....

它没有显示findelemntbyid的建议......没有显示...... :(

我在onAreate of MainActivity中创建按钮 程序崩溃!!! :(

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
..
...
..  
btnADD=(Button)this.findViewById(R.id.btnAdd);
btnADD.setOnClickListener(new clickListener());
}
.
class clickListener implements View.OnClickListener
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}
.
 }

我必须创建我的Button并调用它的OnClickListener ???

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您可以在滑动菜单中添加一个包含listview的布局,这样您就可以在滑动菜单之前/之后添加视图。例如。

    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:orientation="vertical" >
    <TextView
         android:id="@+id/user_fc"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0.04"
         android:gravity="left|center_vertical"
         android:layout_marginLeft="3dp"
         android:textStyle="italic" />
 <ListView
         android:id="@+id/slider_list"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@android:color/white"
         android:choiceMode="singleChoice"
         android:divider="@android:color/black"
         android:dividerHeight="1dp" />

    </LinearLayout>

textView将出现在滑动菜单列表视图之前。

答案 1 :(得分:0)

答案是在this教程...在“FindPeopleFragment”类和“onCreateView”函数中,我必须像这样编写

  btn=(Button)rootView.findViewById(R.id.btnTost);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

如你所见,我们没有“这个”对象听到....我们有“rootView”