Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.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 文本视图在片段中不可见?_Android_Android Fragments_Textview - Fatal编程技术网

Android 文本视图在片段中不可见?

Android 文本视图在片段中不可见?,android,android-fragments,textview,Android,Android Fragments,Textview,在此基础上,我学习了如何在Android中使用Fragments在操作栏中创建选项卡。虽然标签显示得很好,但是代码>文本视图< /代码>应该出现在片段< /代码> s的中间。我看过其他与此相关的StackOverflow问题,但没有一个解决了我的问题 FragmentTab1.java: public class FragmentTab1 extends Fragment { final String TAG = "FRAGMENT_TAB_1"; public View on

在此基础上,我学习了如何在Android中使用
Fragment
s在
操作栏中创建选项卡。虽然标签显示得很好,但是<>代码>文本视图< /代码>应该出现在<代码>片段< /代码> s的中间。我看过其他与此相关的StackOverflow问题,但没有一个解决了我的问题

FragmentTab1.java:

public class FragmentTab1 extends Fragment {
    final String TAG = "FRAGMENT_TAB_1";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab, container, false);
        TextView textview = (TextView) view.findViewById(R.id.txtvwTab);
        textview.setText("one");

        Log.i(TAG, "textview's text: " + textview.getText());

        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <TextView
        android:id="@+id/txtvwTab" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" //added later
        android:textColor="@color/Black" //added later
        android:textSize="40sp" //added later
        android:alpha="1" //added later
        android:text="DEFAULT" //added later
        />

</RelativeLayout>
public class Main extends Activity {
    ActionBar.Tab tab1, tab2, tab3;

    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();

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

        //Remove status bar
        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        tab1 = actionBar.newTab().setText("1");
        tab2 = actionBar.newTab().setText("2");
        tab3 = actionBar.newTab().setText("3");

        tab1.setTabListener(new TabListener(fragmentTab1));
        tab2.setTabListener(new TabListener(fragmentTab2));
        tab3.setTabListener(new TabListener(fragmentTab3));

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
    }
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.novoloc.upup2.Main" />
public class TabListener implements ActionBar.TabListener {
    Fragment fragment;

    public TabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // nothing done here
    }
}
最奇怪的是,虽然
TextView
不可见,但当我查看
LogCat
时,它仍然显示“TextView的text:one”。显然,
TextView
仍在创建中。根本没有错误消息

tab.xml:

public class FragmentTab1 extends Fragment {
    final String TAG = "FRAGMENT_TAB_1";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab, container, false);
        TextView textview = (TextView) view.findViewById(R.id.txtvwTab);
        textview.setText("one");

        Log.i(TAG, "textview's text: " + textview.getText());

        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <TextView
        android:id="@+id/txtvwTab" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" //added later
        android:textColor="@color/Black" //added later
        android:textSize="40sp" //added later
        android:alpha="1" //added later
        android:text="DEFAULT" //added later
        />

</RelativeLayout>
public class Main extends Activity {
    ActionBar.Tab tab1, tab2, tab3;

    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();

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

        //Remove status bar
        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        tab1 = actionBar.newTab().setText("1");
        tab2 = actionBar.newTab().setText("2");
        tab3 = actionBar.newTab().setText("3");

        tab1.setTabListener(new TabListener(fragmentTab1));
        tab2.setTabListener(new TabListener(fragmentTab2));
        tab3.setTabListener(new TabListener(fragmentTab3));

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
    }
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.novoloc.upup2.Main" />
public class TabListener implements ActionBar.TabListener {
    Fragment fragment;

    public TabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // nothing done here
    }
}
活动\u main.xml:

public class FragmentTab1 extends Fragment {
    final String TAG = "FRAGMENT_TAB_1";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab, container, false);
        TextView textview = (TextView) view.findViewById(R.id.txtvwTab);
        textview.setText("one");

        Log.i(TAG, "textview's text: " + textview.getText());

        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <TextView
        android:id="@+id/txtvwTab" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" //added later
        android:textColor="@color/Black" //added later
        android:textSize="40sp" //added later
        android:alpha="1" //added later
        android:text="DEFAULT" //added later
        />

</RelativeLayout>
public class Main extends Activity {
    ActionBar.Tab tab1, tab2, tab3;

    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();

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

        //Remove status bar
        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        tab1 = actionBar.newTab().setText("1");
        tab2 = actionBar.newTab().setText("2");
        tab3 = actionBar.newTab().setText("3");

        tab1.setTabListener(new TabListener(fragmentTab1));
        tab2.setTabListener(new TabListener(fragmentTab2));
        tab3.setTabListener(new TabListener(fragmentTab3));

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
    }
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.novoloc.upup2.Main" />
public class TabListener implements ActionBar.TabListener {
    Fragment fragment;

    public TabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // nothing done here
    }
}

我面临着类似的问题。但我发现textview在那里,但它隐藏在扩展工具栏/操作栏下(因为它包含tablayout)。给你的textview一个100dp或200dp的margintop,并给它一个背景色,你就能看到它在那里,但隐藏在actionBar下。或者你甚至可以通过向上滑动动作栏来收缩它,以显示你的文本视图

如何解决此问题,请参见以下问题:

一直为此头疼,终于找到了一个简单的解决办法。将您的ViewPager高度设置为绝对值,而不是
android:layout\u height=“match\u parent”
android:layout\u height=“wrap\u content”

尝试android:layout\u height=“300dp
或一些首选值


嘿,一次小的尝试,而不是
RelativeLayout
尝试
LinearLayout
,然后将
方向设置为
垂直
,然后查看结果:-/也没有运气(这一定是高度和/或宽度的问题。为相对布局设置背景色,然后为textview设置对比色,查看它们是否绘制。还可以尝试在
dp
中设置实心高度和宽度,请参见我已尝试将宽度设置为300dp,高度设置为150dp。我还将相对视图设置为显式白色。还是不走运。:(@color/Black的值是多少?我想知道这个映射到的任何颜色是否与您的背景色相同?