简体   繁体   English

如何从其他活动更改TextSize

[英]How to change TextSize from a different activity

I am displaying text using viewpager. 我正在使用viewpager显示文本。 I want to change the textsize of the text displayed. 我想更改显示的文本的文本大小。 The problem i am having is that the button is in class which extends Activity which sets adapter to viewpager and the Textview for which i want to change the size is in different class which extends PagerAdapter. 我遇到的问题是该按钮在扩展Activity的类中,该Activity将适配器设置为viewpager,而我要更改其大小的Textview在另一类中扩展了PagerAdapter。

This is the code i have tried. 这是我尝试过的代码。 My main class: 我的主班:

public class NewsDisplay extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_pager);

.
.
.
.
 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

 adapter = new NewsSlider(NewsDisplay.this, newsList);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);
   }

} }

This is options menu from where user clicks to change textsize: 这是用户从中单击以更改文本大小的选项菜单:

public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
         MenuInflater menuInflater = getMenuInflater();
         menuInflater.inflate(R.menu.menu, menu);
         return true;
   }  

This is where i handle click event: 这是我处理点击事件的地方:

public boolean onOptionsItemSelected(MenuItem item)
    {

       switch (item.getItemId())
        {
        case R.id.menu_textSize:
            NewsSlider.news_content.setTextSize(25);
            return true;

        }
    } 

And this is my class which extends PagerAdapter: 这是我的类,它扩展了PagerAdapter:

public class NewsSlider extends  PagerAdapter {

Context context;

 LayoutInflater inflater;
 private ArrayList<HashMap<String, Object>> newsListArray;
 public static TextView news_content;  // This is the textview size i want to change

public NewsSlider(Context context,ArrayList<HashMap<String, Object>> d)  {

    newsListArray = d;
    inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
.
.
.
public Object instantiateItem(ViewGroup container, int position) {

news_content = (TextView) itemview.findViewById(R.id.news_description);
Spanned s = Html.fromHtml(newsinfo.get("news_content").toString());

    news_content.setText(s);
}

When i am clicking textsize menu, the size is not changing. 当我单击textsize菜单时,大小没有变化。 Itried creating options menyu in pageradapter but its giving me error. 尝试在pageradapter中创建选项menyu,但它给了我错误。 Please help me to solve this issue... 请帮我解决这个问题...

The first problem is keeping a static reference to anything in your UI. 第一个问题是保持对UI中任何内容的静态引用。

public static TextView news_content; 公共静态TextView news_content;

Setting that will make sure your whole view and Activity, and anything associated with it, will stay in memory until your whole app process is shut down. 进行设置以确保您的整个视图和活动以及与之关联的所有内容都将保留在内存中,直到关闭整个应用程序进程为止。 This falls under the category of things you CAN do, but never SHOULD do. 这属于您可以做的事情,但绝不应该做。

Second problem, related to this specifically, is that its a ViewPager. 与之特别相关的第二个问题是它的ViewPager。 It'll inflate multiple views, assuming you have multiple pages, so the last TextView inflated will be the one you're calling. 假设您有多个页面,它将膨胀多个视图,因此最后膨胀的TextView将是您要调用的那个。 By design, ViewPager will create the view before and after the one you're currently viewing, so you're pretty much guaranteed to never see this work visually, except MAYBE if you scroll to the last view, and then hit the menu. 根据设计,ViewPager会在当前正在查看的视图之前和之后创建视图,因此,几乎可以保证永远不会看到该视图,除非滚动到最后一个视图,然后单击菜单,否则可以。

What I would do is post some kind of message to a bus when you click the menu, and have your instantiated pages listening for that message. 我要做的是,当您单击菜单时,将某种消息发布到总线上,并使实例化页面监听该消息。 You can use LocalBroadcastManager, but I'd probably use greenrobot. 您可以使用LocalBroadcastManager,但我可能会使用greenrobot。 In any case, register for events in instantiateItem, and unregister in destroyItem. 无论如何,请在InstantiateItem中注册事件,然后在destroyItem中注销。

Another thing you might need to consider is if the html includes some kind of text size info, in which case, it might override your settings, although I'm not sure how that works in the Html.fromHtml case. 可能需要考虑的另一件事是,如果html包含某种文本大小信息,则在这种情况下,它可能会覆盖您的设置,尽管我不确定Html.fromHtml情况下的工作方式。

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

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