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

时间:2013-02-11 06:45:33

标签: android android-fragments android-actionbar android-tabs

我喜欢使用标签创建应用程序。我的应用程序中有三个选项卡,每个选项卡中都有一些按钮。还有三个片段。当按下每个选项卡中的按钮时,我喜欢在常见的TextView中显示一些文本。我的标签工作正常。但我有三个问题。 1.如何为所有选项卡(可能在选项卡外部)提供通用TextView 我片段中的按钮方法不起作用 3.当我尝试显示cutom语言字体时,它给出了一个错误“方法getAssets()未定义为FragmentA类型”

以下是我的主要活动

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

    }

}}

这是我在其中一个片段中的代码

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("അ");            

}}

我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

最后我得到了解决方案。我们可以在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>

现在按钮不需要从片段调用。它可以通过提供FindVuewById方法从Main Acivity直接调用。 Tab1.XMl,Tab2.xml等中的Button名称应该不同。代码如下,

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

}

方法butn1对应于Tab1中的Button1,我们可以为其他标签中的其他按钮提供。