我正在尝试实现一个连续滚动的文本视图。
但是文本没有滚动。我希望文本以以下格式显示:
这是我写的代码。
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代码,它是调用文本视图的正确方法吗?