0

我很难让我TextView的 to always ellipsize="marquee"。我正在使用SherlockActionBar标签和片段。以下是源代码:

TabbedDataViewer.java(这段代码取自ABS自带的例子)

public class TabbedDataViewer extends SherlockFragmentActivity{
      
    private TabHost mTabHost;
    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;    
      
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabbed_data_viewer_main);
        
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager) findViewById(R.id.pager);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        mTabsAdapter.addTab(
                mTabHost.newTabSpec(Constants.TAB1_ID).setIndicator(
                        getString(R.string.personal_data)), PersonalDataTab.class, null);

        if(savedInstanceState != null){
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }
        
    public static class TabsAdapter extends FragmentPagerAdapter implements
            TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo{
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args){
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory{
            private final Context mContext;

            public DummyTabFactory(Context context){
                mContext = context;
            }

            @Override
            public View createTabContent(String tag){
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager){
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args){
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }

        @Override
        public int getCount(){
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position){
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        @Override
        public void onTabChanged(String tabId){
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){
        }

        @Override
        public void onPageSelected(int position){
            // Unfortunately when TabHost changes the current tab, it kindly
            // also takes care of putting focus on it when not in touch mode.
            // The jerk.
            // This hack tries to prevent this from pulling focus out of our
            // ViewPager.
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onPageScrollStateChanged(int state){
        }
    }            
}

个人数据表.java

public class PersonalDataTab extends SherlockFragment{

    @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.activity_personal_data_tab, container, false);

        TextView tvFirstName = (TextView) v.findViewById(R.id.tv_first_name);
        tvFirstName.setText(TabbedDataViewer.sDataMap.get(Constants.TAG_FIRST_NAME));
        
        return v;
    }
}

activity_tabbed_data_viewer_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost 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="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>    
</TabHost>

activity_personal_data_tab.xml

<ScrollView 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" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/first_name_c_s"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tv_first_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>    
</ScrollView>

我已经尝试了这里描述的所有东西有没有办法让 ellipsize="marquee" 总是滚动?- 当我只使用tabHost(否SherlockActionBarFragment)时,这些解决方案对我有用。

顺便说一句,刚刚注意到在PlayMarket 应用程序中,太长而无法在屏幕上显示的应用程序名称根本不会滚动。

如果有人能给我一个关于如何TextView从课堂更新的提示,我将不胜感激,PersonalDataTab.java因为现在我只能从主课堂更新它,即TabbedDataViewer

4

2 回答 2

0

试试这个:

tvFirstName.setSelected(true);

编辑:

在您的 TextView XML 上添加此参数:

            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="left"
            android:marqueeRepeatLimit="marquee_forever"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
于 2013-02-14T14:12:54.167 回答
0

通过将权重设置为 textViews 解决了滚动问题:

<TableRow
    android:id="@+id/tableRow5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/address_c"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tv_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</TableRow>

...以及活动中的一行:tvFirstName.setSelected(true);

于 2013-02-14T21:01:09.577 回答