TextView設置固定高度,當內容過長的時候則會顯示不全。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ScrollViewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15sp"
android:layout_margin="10dp"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
以加載出師表爲例,如圖:
解決方法 將TextView設爲可滑動的 。
在xml中 TextView 中添加
android:scrollbars="vertical"
在頁面代碼中添加
mTvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
重新運行程序即可看到效果
那麼問題又來了,當它被ScrollView包裹的時候會不會生效呢,添加幾個ImageView,如圖:
可以看出,拖動TextView沒有了滾動TextView的效果。那麼,怎麼實現拖動TextView實現滾動TextView的效果呢?
給TextView添加一個監聽事件
mTvContent.setOnTouchListener(onTouchListener);
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//父節點攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
};
運行程序 問題解決
完整代碼
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ScrollViewActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15sp"
android:layout_margin="10dp"
android:scrollbars="vertical"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon_sky"
android:scaleType="fitXY"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon_mountain"
android:scaleType="fitXY"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon_mill"
android:scaleType="fitXY"
/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
public class ScrollViewActivity extends AppCompatActivity {
@BindView(R.id.tv_content)
TextView mTvContent;
private String content = "\n" +
" 出師表\n" +
"\n" +
"\n" +
" 先帝創業未半而中道崩殂,今天下三分,益州疲弊,此誠危急存亡之秋也。然侍衛之臣不懈於內,忠志之士忘身於外者,蓋追先帝之殊遇,欲報之於陛下也。誠宜開張聖聽,以光先帝遺德,恢弘志士之氣,不宜妄自菲薄,引喻失義,以塞忠諫之路也。\n" +
"\n" +
" 宮中府中,俱爲一體;陟罰臧否,不宜異同。若有作奸犯科及爲忠善者,宜付有司論其刑賞,以昭陛下平明之理;不宜偏私,使內外異法也。\n" +
"\n" +
" 侍中、侍郎郭攸之、費禕、董允等,此皆良實,志慮忠純,是以先帝簡拔以遺陛下:愚以爲宮中之事,事無大小,悉以諮之,然後施行,必能裨補闕漏,有所廣益。\n" +
"\n" +
" 將軍向寵,性行淑均,曉暢軍事,試用於昔日,先帝稱之曰“能”,是以衆議舉寵爲督:愚以爲營中之事,悉以諮之,必能使行陣和睦,優劣得所。\n" +
"\n" +
" 親賢臣,遠小人,此先漢所以興隆也;親小人,遠賢臣,此後漢所以傾頹也。先帝在時,每與臣論此事,未嘗不嘆息痛恨於桓、靈也。侍中、尚書、長史、參軍,此悉貞良死節之臣,願陛下親之、信之,則漢室之隆,可計日而待也。\n" +
"\n" +
" 臣本布衣,躬耕於南陽,苟全性命於亂世,不求聞達於諸侯。先帝不以臣卑鄙,猥自枉屈,三顧臣於草廬之中,諮臣以當世之事,由是感激,遂許先帝以驅馳。後值傾覆,受任於敗軍之際,奉命於危難之間,爾來二十有一年矣。\n" +
"\n" +
" 先帝知臣謹慎,故臨崩寄臣以大事也。受命以來,夙夜憂嘆,恐託付不效,以傷先帝之明;故五月渡瀘,深入不毛。今南方已定,兵甲已足,當獎率三軍,北定中原,庶竭駑鈍,攘除奸兇,興復漢室,還於舊都。此臣所以報先帝而忠陛下之職分也。至於斟酌損益,進盡忠言,則攸之、禕、允之任也。\n" +
"\n" +
" 願陛下託臣以討賊興復之效,不效,則治臣之罪,以告先帝之靈。若無興德之言,則責攸之、禕、允等之慢,以彰其咎;陛下亦宜自謀,以諮諏善道,察納雅言,深追先帝遺詔。臣不勝受恩感激。\n" +
"\n" +
" 今當遠離,臨表涕零,不知所言。\n";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
ButterKnife.bind(this);
initView();
}
private void initView() {
mTvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
mTvContent.setOnTouchListener(onTouchListener);
mTvContent.setText(content);
}
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//父節點攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
};
}