文本视图不滚动 [英] text view is not scrolling

查看:40
本文介绍了文本视图不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个连续滚动的文本视图.

I am trying to implement a text view which scrolls continuously.

但是文本没有滚动.我希望文本以下列格式显示:

But the text is not scrolling. I want the text to be displayed in the following format:

这是我写的代码.

public class ScrollingTextView extends TextView {
    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollingTextView(Context context) {
        super(context);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

这是我的 xml 代码.

This is my xml code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:background="@color/background"
        android:orientation="vertical" >

        <org.fluturasymphony.recommendation.ScrollingTextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="25dip"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Really Long Scrolling Text Goes Here.... ..... ............ .... ...." />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="170dp"
            android:background="@color/background"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/outlet_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/outlet_id_text"
                android:inputType="textAutoComplete" />

            <ImageView
                android:id="@+id/search_button"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:adjustViewBounds="true"
                android:contentDescription="@string/cd_search_icon"
                android:src="@drawable/search_icon" />
        </LinearLayout>

        <Button
            android:id="@+id/view_stores_on_map"
            android:layout_width="221dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="76dp"
            android:onClick="showStoreList"
            android:text="@string/view_stores_on_map" />

        <Button
            android:id="@+id/view_stores"
            android:layout_width="221dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="38dp"
            android:onClick="showStoreList"
            android:text="View List Of Stores" />
    </LinearLayout>

</RelativeLayout>

谁能告诉我如何实现这个?同样,我需要的是让红框中的代码连续滚动.

Can anyone tell me how to implement this? Again, what I need is for the code in the red box to scroll continuously.

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

      final Button view_stores = (Button) findViewById(R.id.view_stores);
      final Button view_stores_on_map = (Button) findViewById(R.id.view_stores_on_map);
      final EditText outlet_id = (EditText) findViewById(R.id.outlet_id);
      final ImageView search_button = (ImageView) findViewById(R.id.search_button);

      view_stores.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
            // Load the stores
           Intent myIntent = new Intent(HomeActivity.this, StoreListActivity.class);            
           startActivity(myIntent);
           HomeActivity.this.startActivity(myIntent);
         }
      });
      TextView tv;

      tv = (TextView) findViewById(R.id.textId);----------->***text view***
      tv.setSelected(true);

      search_button.setClickable(true);
      search_button.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        String outlet_no = outlet_id.getText().toString();
        if(!outlet_no.isEmpty()){
        SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();

        prefsEditor.putString("outlet_id", outlet_no);
        prefsEditor.commit();

        Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);       
        startActivity(myIntent);
        HomeActivity.this.startActivity(myIntent);
        }
        else{
          Toast.makeText(getApplicationContext(), "Please enter an outlet id", Toast.LENGTH_SHORT);
        }  
      }
    });

    view_stores_on_map.setOnClickListener(new Button.OnClickListener(){

      @Override
      public void onClick(View v){
        SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();

        prefsEditor.putString("show_all", "1");
        prefsEditor.commit();
        Intent myIntent = new Intent(HomeActivity.this, StoreMapActivity.class);       
        startActivity(myIntent);          
      }
    });
    }

    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.set_client:     final CharSequence[] items = {"colgate", "godrej", "parle"};
                                      AlertDialog.Builder builder = new AlertDialog.Builder(this);
                                      builder.setTitle("Set the client");
                                      builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int item) {
                                          SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
                                          SharedPreferences.Editor prefsEditor = myPrefs.edit();

                                          prefsEditor.putString("client",(String) items[item]);
                                          prefsEditor.commit(); 
                                        }
                                       });
                                      AlertDialog alert = builder.create();
                                      alert.show();

                                      break;            
        }
        return true;
    }
    */   
}

这是我调用文本视图的 Java 代码,这是调用文本视图的正确方法吗?

This is my java code from where i am calling the text view is it the right way to call the text view?

推荐答案

试试这个:

<TextView
            android:id="@+id/textId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:lines="1"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Simple application that shows how to use marquee, with a long text"
            android:textColor="#ff4500" />

从活动中执行此操作:

    TextView tv;

    tv = (TextView) findViewById(R.id.textId);
    tv.setSelected(true);

谢谢.

这篇关于文本视图不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆