Scrollview重新调整textView

时间:2016-01-16 13:57:33

标签: android xml scrollview

我有一个包含许多文本视图的空白活动,因此我想使用scrollview,但每当我使用它时,文本视图从其预先分配的位置转移到顶部。Before adding ScrollViewAfter adding ScrollView这里是xml代码

<?xml version="1.0" encoding="utf-8"?>


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OFF"
    android:id="@+id/button"
    android:onClick="torchToggle"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:background="#cdc6c6" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Torch"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#e8d9d9" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textColor="#dacece"
    android:text="#Error In display" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView3"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="83dp"
    android:textColor="#cdc0c0"
    android:text="#Error in display" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView4"
    android:textColor="#f3ef7a"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:text="#Error in display" />

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="next"
    android:id="@+id/button2"
    android:layout_alignTop="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:nestedScrollingEnabled="false"
    android:onClick="nextActivity" />



</RelativeLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:2)

RelativeLayout位于wrap_content内时,ScrollView的高度会回落到ScrollView,因为match_parent的虚拟高度为无限且android:fillViewport="true"没有&# 39;没有意义。

您可以尝试将ScrollView添加到RelativeLayout。这将使match_parent的最小高度为RelativeLayout。您应该将layout_height&#39; wrap_content更改为 private void scaleImage(ImageView view, int boundBoxInDp) { // Get the ImageView and its bitmap Drawable drawing = view.getDrawable(); Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); // Get current dimensions int width = bitmap.getWidth(); int height = bitmap.getHeight(); // Determine how much to scale: the dimension requiring less scaling is // closer to the its side. This way the image always stays inside your // bounding box AND either x/y axis touches it. float xScale = ((float) boundBoxInDp) / width; float yScale = ((float) boundBoxInDp) / height; float scale = (xScale <= yScale) ? xScale : yScale; // Create a matrix for the scaling and add the scaling data Matrix matrix = new Matrix(); matrix.postScale(scale, scale); // Create a new bitmap and convert it to a format understood by the ImageView Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); BitmapDrawable result = new BitmapDrawable(scaledBitmap); width = scaledBitmap.getWidth(); height = scaledBitmap.getHeight(); // Apply the scaled bitmap view.setImageDrawable(result); // Now change ImageView's dimensions to match the scaled image LinearLayout.LayoutParams params (LinearLayout.LayoutParams)view.getLayoutParams(); params.width = width; params.height = height; view.setLayoutParams(params); }