[英]How to create Scroll Layout?
我對ScrollView有問題。 這是我的XMl文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_weight="0.2"
android:id="@+id/imageView2"
android:adjustViewBounds="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:text=""
android:textColor="#ffff"
android:id="@+id/body" />
</LinearLayout>
</ScrollView>
這是活動:
public class NewsBodyFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_body_fragment, container, false);
ImageView image = (ImageView) v.findViewById(R.id.imageView2);
TextView textView = (TextView) v.findViewById(R.id.body);
//textView.setMovementMethod(new ScrollingMovementMethod());
String body = getArguments().getString("body");
byte[] bmpArray = getArguments().getByteArray("image");
if(bmpArray != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(bmpArray, 0, bmpArray.length);
image.setImageBitmap(bmp);
}
else
{
image.setImageDrawable(getResources().getDrawable(R.drawable.empirelogo));
}
textView.setText(body);
return v;
}
}
我的問題是ScrollView,當我使用此代碼時,ScrollView無法滾動。 Image和TextView應該看起來像這樣,它們應該一起滾動:
http://s18.postimg.org/5tu7dsmpl/3_E6_ABE7_F385_F5_EE469_EDD28_D9_C26055156_C987_E0_D5_D02_D7.jpg
但就我而言,它們不會滾動。 當我使用ScrollView android:layout_height =“ wrap_content”時。 看起來像這樣:
http://s30.postimg.org/e15g3scfl/23_C4_C2_C62_BBCF4_E22_E9_B2_A04_ED7923278_C418_DA6_CDF7_EF1.jpg
請幫助我知道答案或解決方案的人。
方法:在
onCreate
方法上,獲取ScrollView
高度並將ImageView的高度設置為scrollViewHeight * 0.8:
ScrollView scrollView = (ScrollView)findViewById(R.id.contentScrollView);
ImageView imageView = (ImageView)findViewById(R.id.contentImageView);
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = scrollView.getMeasuredWidth();
int height = scrollView.getMeasuredHeight();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(layoutParams.MATCH_PARENT, height * 0.8f);
imageView.setLayoutParams(layoutParams);
}
});
在這里檢查我的答案,以將OnGlobalLayoutListener
用作獲取ScrollView
高度的位置。
重量值應交換。 還將TextView
高度設置為wrap_content
並刪除weight
:
<ScrollView
android:id="@+id/contentScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/contentImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:adjustViewBounds="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:text=""
android:textColor="#ffff"
android:id="@+id/body" />
</LinearLayout>
</ScrollView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.