Android의 마키 텍스트
Android 애플리케이션에서 선택 윤곽 텍스트를 사용하려면 어떻게해야합니까?
다음은 그 예입니다.
public class TextViewMarquee extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true); // Set focus to the textview
}
}
textview가있는 xml 파일 :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/mywidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:maxLines="1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textColor="#ff4500"
android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>
TextView textView = (TextView) this.findViewById(R.id.textview_marquee);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);
android:ellipsize="marquee"
이것은 TextView
초점 이있을 때만 작동합니다 .
이 작업을 수행하려면 이미 언급 한 세 가지 (ellipsize, selected, singleLine)를 모두 사용해야했습니다.
TextView tv = (TextView)findViewById(R.id.someTextView);
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true):
이 매개 변수를 TextView에 넣으십시오.
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
또한 다음을 수행해야합니다 setSelected(true)
.
my_TextView.setSelected(true);
인사, 크리스토퍼
XML 코드
<TextView
android:id="@+id/txtTicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:scrollHorizontally="true"
android:shadowColor="#FF0000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
자바
txtEventName.setSelected(true);
텍스트가 작 으면 텍스트 앞뒤에 공백 추가
txtEventName.setText("\t \t \t \t \t \t"+eventName+"\t \t \t \t \t \t");
짧은 문자열에는 사용할 수 없다는 marquee 문제를 발견했습니다 (기능은 긴 문자열 만 표시하는 것이므로).
짧은 문자열을 수평으로 이동하려면 Webview 사용을 제안합니다. Main_Activity.java 코드 :`
WebView webView;
webView = (WebView)findViewById(R.id.web);
String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
+ "Hello Droid" + "</marquee></FONT></html>";
webView.loadData(summary, "text/html", "utf-8"); // Set focus to the textview
`
main_activity.xml 코드 :
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/web"
></WebView>
droidgren에 의해 식별 된 XML 설정뿐만 아니라 내 테스트에 따르면 표시하려는 텍스트가 textview의 너비보다 짧으면 선택 윤곽이 전혀 스크롤되지 않는 것으로 나타났습니다. 가능한 해결책은보기의 너비를 텍스트 길이보다 작은 크기로 설정하거나 스크롤이 정상적으로 보이도록 적절한 공백을 사이에두고 문자열을 자체에 2 ~ 3 회 연결하는 것입니다.
XML에서 코드 아래에 추가
<TextView
android:text="Shops NearBy textdf fsdgsdgsdg dsgtsgsdgsdgsg"
android:id="@+id/txtEventName"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:singleLine="true"/>
Java에서 다음 코드를 추가하십시오.
TextView txtEventName=(TextView)findViewById(R.id.txtEventName);
txtEventName.setSelected(true);
XML 파일에서 선택 윤곽과 같은 기능을 얻으려면 TextView에 다음과 같은 추가 속성을 추가해야합니다.
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
MainActivity.java 파일에서 findViewById ()를 사용하여이 TextView의 참조를 가져올 수 있으며이 TextView에 다음 속성을 설정하여 마키 텍스트처럼 보이게 할 수 있습니다.
setSelected(true);
그게 전부입니다.
위의 모든 것을 시도했지만 저에게는 작동하지 않았습니다. 내가 추가 할 때
android:clickable="true"
그런 다음 완벽하게 작동합니다. 이유를 모르겠습니다. 그러나 나는 그것을 일하게되어 기쁩니다.
여기에 나의 완전한 대답이 있습니다.
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
이것을 사용하여 Marque를 설정합니다.
final TextView tx = (TextView) findViewById(R.id.textView1);
tx.setEllipsize(TruncateAt.MARQUEE);
tx.setSelected(true);
tx.setSingleLine(true);
tx.setText("Marquee needs only three things to make it run and these three things are mentioned above.");
" android : marqueeRepeatLimit ="marquee_forever "를 xml 파일 에 사용할 필요가 없습니다 . Marquee는이 없이도 작동합니다.
이것은 "end"와 동일합니다.
where = TruncateAt.END
TextView 또는 사용자 정의 TextView를 사용할 수 있습니다. 후자는 textview가 항상 초점을 맞출 수없는 경우입니다.
먼저 다음과 같이 레이아웃 .xml 파일의 스크롤 텍스트보기로 TextView 또는 사용자 지정 TextView를 사용할 수 있습니다.
<com.example.myapplication.CustomTextView
android:id="@+id/tvScrollingMessage"
android:text="@string/scrolling_message_main_wish_list"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/black"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:freezesText="true"/>
참고 : 위 코드 스 니펫에서 com.example.myapplication은 예제 패키지 이름이며 사용자 고유의 패키지 이름으로 바꿔야합니다.
그런 다음 CustomTextView를 사용하는 경우 CustomTextView 클래스를 정의해야합니다.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@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;
}
}
Hope it will be helpful to you. Cheers!
With the above answer, you cannot set the speed or have flexibility for customizing the text view functionality. To have your own scroll speed and flexibility to customize marquee properties, use the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:lines="1"
android:id="@+id/myTextView"
android:padding="4dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Simple application that shows how to use marquee, with a long text" />
Within your activity:
private void setTranslation() {
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, 0.0f);
tanim.setDuration(1000);//set the duration
tanim.setInterpolator(new LinearInterpolator());
tanim.setRepeatCount(Animation.INFINITE);
tanim.setRepeatMode(Animation.ABSOLUTE);
textView.startAnimation(tanim);
}
This is my xml customTextView Object here you can use simply TextView to replace on Tag.
<com.wedoapps.crickethisabkitab.utils.view.montserrat.CustomTextView
android:id="@+id/lblRateUsPlayStore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_10sdp"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_5sdp"
android:text="@string/please_rate_us_5_star_on_play_store"
android:textAllCaps="false"
android:textColor="@color/green"
android:textSize="@dimen/_25ssp"
android:textStyle="bold"
android:visibility="visible"
android:linksClickable="true"
android:autoLink="web|phone"/>
And here is My Java File code. i have set my html text on server just replace your text on textview object. i have put this code is marquee tag with clickable if any links on this textview to open mobile or webBrowser.
CustomTextView lblRateUsPlayStore = findViewById(R.id.lblRateUsPlayStore);
lblRateUsPlayStore.setMovementMethod(LinkMovementMethod.getInstance());
lblRateUsPlayStore.setText( Html.fromHtml(documentSnapshot.getString("DisplayText")));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(lblRateUsPlayStore, 12, 20, 2, 1);
lblRateUsPlayStore.setEllipsize(TextUtils.TruncateAt.MARQUEE);
// Set marquee repeat limit (unlimited)
lblRateUsPlayStore.setMarqueeRepeatLimit(-1);
lblRateUsPlayStore.setHorizontallyScrolling(true);
lblRateUsPlayStore.setSelected(true);
lblRateUsPlayStore.setLinksClickable(true);
lblRateUsPlayStore.setFocusableInTouchMode(true);
lblRateUsPlayStore.setFocusable(true);
Lots of answers correctly state that calling textView.setSelected(true)
is required. However, if preferred to do so just via XML, one option could be to make use of the Binding Adapters when working with Data Binding.
So, just create a new adapter similar to:
@BindingAdapter("app:autoStartMarquee")
fun setAutoStartMarquee(textView: TextView, autoStartMarquee: Boolean) {
textView.isSelected = autoStartMarquee
}
Then, you can simply use it in the XML as follows:
...
<TextView
...
android:ellipsize="marquee"
android:singleLine="true"
app:autoStartMarquee="@{true}"/>
...
No need to call it from code anymore.
You can use
android:ellipsize="marquee"
with your textview.
But remember to put focus on the desired textview.
참고URL : https://stackoverflow.com/questions/2182578/marquee-text-in-android
'Nice programing' 카테고리의 다른 글
iOS에서 UIWebView의 배경을 변경할 수 없습니다. (0) | 2020.12.09 |
---|---|
initWithImage : (UIImage *) image를 사용하여 UIBarButtonItem에 이미지 추가 (0) | 2020.12.09 |
Python의 matplotlib에서 경험적 cdf를 그리는 방법은 무엇입니까? (0) | 2020.12.09 |
com.android.builder.model.AndroidProject 인터페이스의 클래스 경로를 확인할 수 없습니다. (0) | 2020.12.09 |
WPF 버튼 이미지 '회색'? (0) | 2020.12.09 |