Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击imagebutton更改片段_Android_Android Fragments - Fatal编程技术网

Android 单击imagebutton更改片段

Android 单击imagebutton更改片段,android,android-fragments,Android,Android Fragments,我是android新手。我正在尝试创建一个应用程序,上面有三个imagebutton,位于水平线上,下面有一个片段,可以通过点击按钮来更改(与tinder android应用程序相同)。我试过使用两个不同的片段,一个有三个图像按钮,另一个在下面)。然后,我还尝试对图像使用gridview,然后在下面的部分使用片段。我还尝试过简单地使用图像和动态更改片段。我有不同的错误导致崩溃。我想知道的是,我如何才能以最有效的方式实现我的目标 编辑:当我只使用没有gridview的图像或imagebuttons

我是android新手。我正在尝试创建一个应用程序,上面有三个
imagebutton
,位于水平线上,下面有一个片段,可以通过点击按钮来更改(与tinder android应用程序相同)。我试过使用两个不同的片段,一个有三个图像按钮,另一个在下面)。然后,我还尝试对图像使用
gridview
,然后在下面的部分使用片段。我还尝试过简单地使用图像和动态更改片段。我有不同的错误导致崩溃。我想知道的是,我如何才能以最有效的方式实现我的目标

编辑:当我只使用没有
gridview
的图像或
imagebuttons
的片段的图像时,代码如下

package com.psycho.ayush.sahanubhooti_20;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    ImageButton ngosButton, profileButtton, donateButton;
    NGOsFragment ngosFragment;
    ProfileFragment profileFragment;
    DonateFragment donateFragment;

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

        ngosButton = (ImageButton)findViewById(R.id.ngoButton);
        profileButtton = (ImageButton)findViewById(R.id.profileButton);
        donateButton = (ImageButton)findViewById(R.id.donateButton);

        ngosButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragTrans = getSupportFragmentManager().beginTransaction();
                fragTrans.add(R.id.fragment_container, ngosFragment);
                fragTrans.addToBackStack(null);
                fragTrans.commit();
            }
        });
    }

}

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/fragment_container"
    tools:context="com.psycho.ayush.sahanubhooti_20.MainActivity">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ngoButton"
        android:src="@drawable/ngo"
        android:background="@android:color/transparent" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profileButton"
        android:layout_gravity="center_horizontal|top"
        android:src="@drawable/profile"
        android:background="@android:color/transparent" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/donateButton"
        android:layout_gravity="right|top"
        android:src="@drawable/donate"
        android:background="@android:color/transparent" />

</FrameLayout>

设置所有按钮上的click listener,并设置下面的代码以设置框架布局上的不同片段。框架布局id是您在“活动”中设置的框架布局的资源id,Fragment类是您创建的要在单击图像按钮时调用的片段

 getSupportFragmentManager().beginTransaction().replace(R.id.frame layout id, Fragment class name).commit()

设置所有按钮上的click listener,并设置下面的代码以设置框架布局上的不同片段。框架布局id是您在“活动”中设置的框架布局的资源id,Fragment类是您创建的要在单击图像按钮时调用的片段

 getSupportFragmentManager().beginTransaction().replace(R.id.frame layout id, Fragment class name).commit()

共享相关代码+错误日志。您应该真正使用tablayout。您可以查看此链接:谢谢。我不知道这是怎么回事。我已经添加了日志和相关代码来堆栈溢出!我尽我所能地编辑了你的问题。然而,。请格式化并编辑您遇到的特定错误消息,以防有必要识别特定问题,以便更多了解该主题的人看到它。祝你好运共享相关代码+错误日志。您应该真正使用tablayout。您可以查看此链接:谢谢。我不知道这是怎么回事。我已经添加了日志和相关代码来堆栈溢出!我尽我所能地编辑了你的问题。然而,。请格式化并编辑您遇到的特定错误消息,以防有必要识别特定问题,以便更多了解该主题的人看到它。祝你好运我已经这样做了,但仍然应用程序崩溃点击该按钮。我已经添加了代码和高于信息级别的日志发布您的activity_main.xml文件代码。我认为问题在于dat。。。对于此类功能,最好使用选项卡布局。通过下面的链接。这是了解选项卡的最佳链接之一。。。。我已经这样做了,但仍然应用程序崩溃点击该按钮。我已经添加了代码和高于信息级别的日志发布您的activity_main.xml文件代码。我认为问题在于dat。。。对于此类功能,最好使用选项卡布局。通过下面的链接。这是了解选项卡的最佳链接之一。。。。