【问题标题】:Android: How to make this "tab switcher"Android:如何制作这个“标签切换器”
【发布时间】:2016-01-19 02:18:44
【问题描述】:

我正在尝试制作这样的“标签切换器”:

http://i.stack.imgur.com/C5DMW.png

http://i.stack.imgur.com/bIGHP.png

我实际上将按钮用于选项卡,将布局用于视图。我将 Layout 的 Visibility 设置为 GONE(当它不活动时),并在我按下 Tab 按钮时设置为 VISIBLE。没事吧?有没有更好的方法来做到这一点?

我不知道我是否应该在布局中使用片段并从不同的活动加载选项卡,或者只是在布局和相同的活动中设计选项卡。

我会给你看一些代码,这样你就可以理解我了:

<LinearLayout 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:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:text="ACTIVITY ONE"
        android:id="@+id/btnActivity1"
        android:background="@drawable/tab_normal"
        android:textStyle="bold" />

    <FrameLayout
        android:id="@+id/layActivity1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:visibility="visible">

        <fragment
            android:name="com.android.tests.fragments.tab1"
            android:id="@+id/fraActivity1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/layout_tab1" />

    </FrameLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:text="ACTIVITY TWO"
        android:id="@+id/btnActivity2"
        android:background="@drawable/tab_normal"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/layActivity2"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:padding="35dp"
        android:visibility="gone">

        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="NEXT"
            android:id="@+id/btnNEXT"
            android:background="@drawable/button_style"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>

</LinearLayout>

在 Activity1 中,我使用 Fragment,在 Activity2 中,我不使用。正确的方法是什么?

代码是这样的:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class Main extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnActivity1 = (Button)findViewById(R.id.btnActivity1);
        Button btnActivity2 = (Button)findViewById(R.id.btnActivity2);
        final FrameLayout layActivity1 = (FrameLayout)findViewById(R.id.layActivity1);
        final LinearLayout layActivity2 = (LinearLayout)findViewById(R.id.layActivity2);

        btnActivity1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layActivity1.setVisibility(View.VISIBLE);
                layActivity2.setVisibility(View.GONE);
            }
        });

        btnActivity2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layActivity1.setVisibility(View.GONE);
                layActivity2.setVisibility(View.VISIBLE);
            }
        });
    }
}

我不知道我是否必须使用“扩展 Activity”或“FragmentActivity”。每条评论都会有所帮助。

对不起,英语不好,希望你能理解。

【问题讨论】:

    标签: android android-fragments tabs


    【解决方案1】:

    我认为您应该使用可扩展布局: https://github.com/traex/ExpandableLayout

    【讨论】:

    • 看起来不错。我喜欢动画。但是我可以以编程方式修改标题文本吗?因为我想显示该字段的实际值:看看“John”如何出现在原始帖子的第二张图片中。
    • 您可以在示例中看到,更改 Header String[] 中的字符串。然后再次setAdapter或者notifidatasetchange
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2017-11-16
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多