0

我想TextView借助 a来控制SeekBarTextView大小必须显示在里面。请帮帮我。SeekBarTextViewTextViewViewPager

以下是我的代码,

public class MainActivity extends FragmentActivity {
    TextView tv;
    SeekBar seek;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seek = (SeekBar) findViewById(R.id.seekBar1);
        /** Getting a reference to the ViewPager defined the layout file */
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        final int PAGE_COUNT = 5;

        /** Constructor of the class */
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /** This method will be invoked when a page is requested to create */
        @Override
        public Fragment getItem(int arg0) {

            MyFragment myFragment = new MyFragment();
            Bundle data = new Bundle();
            data.putInt("current_page", arg0 + 1);
            myFragment.setArguments(data);
            return myFragment;
        }

        /** Returns the number of pages */
        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        public CharSequence getPageTitle(int position) {
            return "Page #" + (position + 1);
        }

    }

    public class MyFragment extends Fragment {
        MainActivity m;
        int mCurrentPage;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            /** Getting the arguments to the Bundle object */
            Bundle data = getArguments();

            /** Getting integer data of the key current_page from the bundle */
            mCurrentPage = data.getInt("current_page", 0);

        }

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

            View v = inflater.inflate(R.layout.myfragment_layout, container,
                    false);
            tv = (TextView) v.findViewById(R.id.tv);

            tv.setText("You are viewing the page #" + mCurrentPage + "\n\n"
                    + "Swipe Horizontally left / right");
            return tv;

        }

    }

}
4

1 回答 1

1

我猜你SeekBar在外面ViewPager。首先OnSeekBarChangeListenerSeekBar:

private OnSeekBarChangeListener mListener = new OnSeekBarChangeListener() {

    // ...

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {         
        if (fromUser) {
            MyFragment mf = (MyFragment) getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem);
            if (mf != null && mf.getView() != null) {
                mf.updateText(progress);
            }
            mf = (MyFragment) getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem - 1);
            if (mf != null && mf.getView != null) {
                mf.updateText(progress);
            }        
            mf = (MyFragment) getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem + 1);
            if (mf != null && mf.getView != null) {
                mf.updateText(progress);
            }        
        }
    }
};

wheremCurrentItemint将存储当前页面的字段ViewPager

private int mCurrentItem = 0;
// ... in the onCreate method
seek.setMax(100); // the maximum for the TextView size
seek.setProgress(50); // the starting textSize for the TextView
seek.setOnSeekBarChangeListener(mListener);

您还需要一个ViewPager.OnPageChangeListener更新ViewPager字段mCurrentItem

pager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            mCurrentItem = position;
            MyFragment mf = (MyFragment) getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.pager + ":" + position);
            if (mf != null && mf.getView() != null) {
                mSeekBar.setOnSeekBarChangeListener(null);
                mSeekBar.setProgress((int) (mf.getTextSize()));
                mSeekBar.setOnSeekBarChangeListener(mListener);
            }
        }
                    // ...
}

不要忘记将此方法添加到您的片段类中:

public void updateText(int progress) {
    textSize = progress;       
        ((TextView)getView().findViewById(R.id.the_textview_id)).setTextSize(textSize);         
}

public float getTextSize() {
   return textSize;
}

mTextsize片段类中将保存文本大小的字段在哪里:

private float textSize = 50;

刚刚意识到我可能误解了这个问题。如果您想更新TextView大小、宽度/高度,那么不要像我的答案那样设置文本,只需LayoutParams从 中检索TextView并根据需要进行更新。

于 2012-11-24T12:25:31.113 回答