1

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

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

在此处输入图像描述

这是我写的代码。

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 代码。

<?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>

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

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代码,它是调用文本视图的正确方法吗?

4

2 回答 2

2

尝试这个 :

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

从 Activity 执行此操作:

    TextView tv;

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

谢谢。

于 2013-01-02T09:51:46.807 回答
2

我不知道您使用自定义滚动文本视图的目的是什么,而您可以将文本视图本身更改为滚动文本视图。你可以这个链接,你可以在其中获得一个在 TextView 内滚动文本的示例。如果在执行此操作后它也没有滚动,那么您只需在 Activity 的 onCreate() 中调用 Textview 实例上的 setSelected(true) 即可。

于 2013-01-02T09:17:06.853 回答