【问题标题】:setting up multiple buttons in fragment在片段中设置多个按钮
【发布时间】:2014-03-10 20:27:50
【问题描述】:

我有一个这样的片段

    public class PagesFragment extends Fragment  {
    public PagesFragment(){}
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.section, container, false);
    return v;
}}

我想设置六个按钮来打开六个相应的 xml 布局。如果有办法在不设置六种不同活动的情况下做到这一点,我很想知道。

xml:

   <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:background="#ffffff"
    android:clickable="true">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff">



        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/art"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/art"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"
             />


        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ext"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/ext"
            android:layout_below="@+id/art"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/inter"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/inter"
            android:layout_below="@+id/ext"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>


        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/sport"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/sport1"
            android:layout_below="@+id/inter"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/opp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/opp"
            android:layout_below="@+id/sport"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/muse"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/muse"
            android:layout_below="@+id/opp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>
    </RelativeLayout>
</ScrollView>

谢谢你。

【问题讨论】:

    标签: java android xml button


    【解决方案1】:

    片段是要走的路。 创建一个包含 FrameLayout (id=example_id) 的布局的活动 (ExampleActivity),该布局将被片段动态替换。

       //inner xml of Example Activity Layout
       <FrameLayout
        android:id="@+id/example_id"
        android:layout_width="match_parent"/>
    

    当从按钮启动意图到 ExampleActivity 时,传递有关您要使用的片段的信息(可能是在工厂中使用的字符串,以便您可以实例化正确的片段)。然后在活动上:

     Fragment fragment;
     //choose the fragment from intent data
     fragment = createFragmentFromIntent(intent);
    
    
     FragmentManager fragmentManager = getChildFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     fragmentTransaction.replace(example_id, fragment);
     fragmentTransaction.commitAllowingStateLoss();
    

    希望对你有帮助。

    【讨论】:

    • 那么按钮在哪里发挥作用
    • 我也在想这样的事情 Button mButton = (Button) v.findViewById(R.id.art); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), Art.class); startActivity(intent); } });
    • 该按钮通过创建意图并传递信息来帮助您决定在示例活动中设置哪个片段。
    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多