简体   繁体   English

如何在操作栏选项卡之外添加TextView

[英]How to add a TextView outside the action bar tabs

I like to make an application with tabs. 我喜欢用标签制作应用程序。 There are three tabs in my application with some buttons in each tab. 我的应用程序中有三个选项卡,每个选项卡中都有一些按钮。 And there are three fragments also. 并且也有三个片段。 I like to display some text in to a common TextView when the buttons in each tab is pressed.My tabs are working fine. 我喜欢在按下每个选项卡中的按钮时在普通TextView中显示一些文本。我的选项卡运行良好。 But I have some three issues. 但是我有三个问题。 1.How to give a common TextView for all the tabs (perhaps outside the tabs) 2.My button method in the fragments dont work 3.When I try to display cutom language fonts it gives me an error "The method getAssets() is undefined for the type FragmentA" 1.如何为所有选项卡(可能在选项卡之外)提供通用的TextView 2.片段中的我的按钮方法不起作用3.当我尝试显示自定义语言字体时,它给我一个错误“方法getAssets()为对于FragmentA类型未定义”

The following is my main activity 以下是我的主要活动

public class MLkeyboardActivity extends Activity {
/** Called when the activity is first created. */
//public static TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlkeyboard);


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




    Tab tabA = actionBar.newTab();
    tabA.setText("Tab A");
    tabA.setTabListener(new TabListener<FragmentA>(this, "Tag A", FragmentA.class));
    tabA.setIcon(R.drawable.ic_launcher);
    actionBar.addTab(tabA);

    Tab tabB = actionBar.newTab();
    tabB.setText("Tab B");
    tabB.setTabListener(new TabListener<FragmentB>(this, "Tag B", FragmentB.class));
    actionBar.addTab(tabB);

    Tab tabC = actionBar.newTab();
    tabC.setText("Tab C");
    tabC.setTabListener(new TabListener<FragmentC>(this, "Tag C", FragmentC.class));
    actionBar.addTab(tabC);

    if (savedInstanceState != null) {
        int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
        getActionBar().setSelectedNavigationItem(savedIndex);
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
}

public static class TabListener<T extends Fragment> 
    implements ActionBar.TabListener{

    private final Activity myActivity;
    private final String myTag;
    private final Class<T> myClass;

    public TabListener(Activity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(myFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(myFragment);
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}}

and this is my code in one of the fragments 这是我的代码中的片段之一

public class FragmentA extends Fragment {
public static TextView textView1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
    return myFragmentView;      
}
public void button1(View view) {
    TextView tv = (TextView) getActivity().findViewById(R.id.textView1);
    Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/AnjaliOldLipi.ttf");
    tv.setTypeface(typeFace);
    tv.setText("അ");            

}}

I will be extremely thankful to you for any help. 我将非常感谢您的帮助。

Finally I got the solution. 终于我找到了解决方案。 We can provide a common TextView in the Main XML file as follows. 我们可以在Main XML文件中提供一个通用的TextView,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Now the button is not required to ba called from fragment. 现在,不需要从片段调用按钮。 It can be directly called from the Main Acivity, by giving the FindVuewById method. 通过提供FindVuewById方法,可以直接从Main Acivity中调用它。 The Button name should be different in Tab1.XMl, Tab2.xml etc. The code is as follows, Tab1.XMl,Tab2.xml等中的按钮名称应不同。代码如下,

public void butn1(View view) {
    TextView tv = (TextView) findViewById(R.id.textView1);      
    tv.setText("abc");  

}

method butn1 corresponds to the Button1 in Tab1, similerly we can give for other buttons in other tabs. 方法butn1对应于Tab1中的Button1,类似地,我们可以为其他选项卡中的其他按钮指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM