【问题标题】:Sizing android tabs and fragments调整 android 选项卡和片段的大小
【发布时间】:2015-12-14 21:42:54
【问题描述】:

我是 Android 新手,但知道如何使用 Java,所以我只需要我的应用程序的一些布局部分的帮助。

基本上我希望在应用程序的顶部有 4 个选项卡(我已经这样做了),当单击每个选项卡时,它会显示一个不同的片段(非常新,所以这可能不正确),这将用于不同的目的。

我不知道如何解决的第一个问题是我希望前三个选项卡(键盘、媒体遥控器和鼠标)大于设置选项卡(如果可能,我会将其更改为图标)。我希望前三个宽度相等,第四个要小很多。

其次,单击相应的选项卡时如何显示不同的片段(例如,单击“MEDIA REMOTE”将打开 mediaRemoteFragment)。该应用程序将是一个客户端,因此需要在后台运行主要部分(MainActivity.java??),然后片段将向主应用发送消息,然后将这些消息发送到服务器。

抱歉,如果这有点不清楚......

这是我当前的代码:

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

import layout.FragmentTab;

public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("keyboardTab").setIndicator("Keyboard", null), FragmentTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("mediaRemoteTab").setIndicator("Media Remote", null), FragmentTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("mouseTab").setIndicator("Mouse", null), FragmentTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("settingsTab").setIndicator("Settings", null), FragmentTab.class, null);

    }
}

FragmentTab.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import kaihulme.tabs.R;

public class FragmentTab extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_layout, container, false);
        TextView tv = (TextView) v.findViewById(R.id.text);
        tv.setText(this.getTag() + " Content");
        return v;
    }
}

activity_main.xml:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

fragment_layout.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="#eaecee">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="hello_world"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

【问题讨论】:

    标签: java android android-fragments tabs fragment


    【解决方案1】:

    我看过你的布局,它们对我来说是正确的。 首先,您必须创建 4 个片段。您需要在选项卡上实现onClik() 侦听器以遵循此过程:当您单击选项卡时,实际的fragment 应该会消失,因此应该将其删除并创建一个新片段并将其插入到@987654324 @。为此,您可以使用方法replace()

    android studio 自动生成的片段模板非常有用。在这里你有很多关于片段的信息:http://developer.android.com/intl/es/guide/components/fragments.html

    最重要的是: 当您需要更换片段时,请移除旧片段并使用 android studio 提供的newInstance() 方法创建新片段,您必须根据需要进行更改。

    KeyboardFragment mFrag = KeyboardFragment.newInstace(arg1, arg2);

    您可以调用片段方法保存已创建片段的实例:

    mFrag.myMethod();

    但是你不能从片段中调用 Activity 方法。要共享信息、通知某些事件等,您必须使用上一篇文章中解释的接口:

    public static class FragmentA extends ListFragment {
        OnArticleSelectedListener mListener;
        ...
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (OnArticleSelectedListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
            }
        }
        ...
    }
    

    Activity 必须实现该接口,然后您可以调用接口方法将片段与容器 Activity 进行通信。

    希望对您开始工作有所帮助。

    【讨论】:

    • 感谢您的回复 - 我会试试这个
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多