文本视图不滚动

时间:2013-01-02 09:07:47

标签: android textview

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

但文字不滚动。 我希望文本以下列格式显示:

enter image description here

这是我写的代码。

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代码,我在这里调用文本视图是否是调用文本视图的正确方法?

2 个答案:

答案 0 :(得分:2)

当您将TextView本身更改为Scroll TextView时,我没有使用自定义滚动TextView的目的。您可以使用link,在这里您可以获得一个示例,以便在TextView中滚动文本。如果在执行此操作后它也不滚动,那么您只需在Activity的onCreate()上的Textview实例上调用setSelected(true)。

答案 1 :(得分:1)

试试这个:

<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);

感谢。