Android的工作室 - 可以添加指向从设计师的片段标签? [英] Android studio - is possible to add tabs pointing to fragments from designer?

查看:342
本文介绍了Android的工作室 - 可以添加指向从设计师的片段标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图增加对主要活动的标签应指向几个片段。

i'm trying to add tabs for main activity which should pointing to several fragments.

是否有可能在Android Studio设计?

Is it possible from Android Studio designer?

我加入像下面图片我的活动布局tabhost,但真实的设备没有显示。

I added into my activity layout tabhost like on image below, but on real device is not displayed.

我怎样才能解决呢?

感谢您的任何意见。

推荐答案

我将减少一个Button我的例子中,你应该能够相乘。我也切出unneccessarry的东西,如利润率等等.​​.....如果你有问题,只是告诉我,我会很乐意帮助你:

I will reduce my example on one Button, you should be able to multiply it. I also cut out unneccessarry stuff like margins etc... If you have problems, just tell me and I will gladly help you out:

MainActivity.java - openHome方法是有趣的。

MainActivity.java - openHome method is interesting

public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener
{
    FragmentManager fragmentManager = getFragmentManager();

    HomeFragment homeFragment;


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

        homeFragment = new HomeFragment();

        fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
    }

    public void openHome(View view)
    {
        homeFragment = new HomeFragment();
        fragmentManager.beginTransaction().replace(R.id.mainFrame, homeFragment).commit();
    }

    @Override
    public void onFragmentInteractionHome(Uri uri)
    {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();//only to check, can be removed (the content, the method must be implemented!!!!!!!!!!!!!!!)
    }
}

activity_main.xml中

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <FrameLayout
        android:id = "@+id/mainFrame"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
        </FrameLayout>

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "@dimen/bottom_Main_Tabs"
        android:layout_gravity = "bottom"
        >

        <ImageButton
            android:id = "@+id/bottomButton_home"
            android:layout_height = "match_parent"
            android:layout_width = "0dp"
            android:layout_weight = "1.0"
            android:background = "@drawable/ic_home_white"
            android:onClick = "openHome"
            />
    </LinearLayout>
</FrameLayout>

HomeFragment.java - 如果你创建一个片段,这一切都将被自动生成加评论,我切出!有趣的是只在底部的内界面!!!

HomeFragment.java - if you create a fragment, all this will be autogenerated plus comments, which I cut out.!!! Interesting is only the inner interface at the bottom!!!:

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class HomeFragment extends Fragment
{
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public static HomeFragment newInstance(String param1, String param2)
    {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public HomeFragment()
    {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
        {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri)
    {
        if (mListener != null)
        {
            mListener.onFragmentInteractionHome(uri);
        }
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnFragmentInteractionListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener
    {
        // TODO: Update argument type and name
        public void onFragmentInteractionHome(Uri uri);
    }

}

fragment_home.xml

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    tools:context="com.domain.app.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:textSize="100sp"
        android:text="Home" />

</FrameLayout>

我tthink这是所有。如果没有,只是告诉我:)

I tthink that is all. If not, just tell me :)

这篇关于Android的工作室 - 可以添加指向从设计师的片段标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆