【问题标题】:How to create Scroll Layout?如何创建滚动布局?
【发布时间】:2015-08-16 13:18:05
【问题描述】:

我对 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

请知道答案或解决方案的人帮助我。

【问题讨论】:

  • 由于 404 导致无法访问图片链接
  • 你为什么还要使用滚动视图?你不希望滚动视图来实现图片链接中的内容吗?

标签: android


【解决方案1】:

方法:获取ScrollView的高度,设置ImageView的高度 在onCreate 上滚动ViewHeight * 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);
    }
});

检查我的答案here,以便使用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>

【讨论】:

  • 如果我有长文本的 TextView。在这种情况下,我需要 ScrollView
  • 图片必须是屏幕高度的0.8?不能只是一个固定的高度。这更合乎逻辑,因为使用大屏幕的用户希望在屏幕上看到更多数据,而不是放大它们。这就是移动开发指南。根据您的回答,我将编辑我的答案:)
  • 关于图片,是的。但是 0.8 的图像应该随着 TextView 滚动,
  • 如果这不起作用,唯一的解决方案是在后面的活动代码中进行。请让我知道这是否不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2019-05-22
相关资源
最近更新 更多