在我旋转屏幕之前,ScrollView内容不完整

时间:2018-01-30 19:10:48

标签: android xml android-layout

我正在使用方法

将我的应用程序记录到ScrollView中
addLog(View view, String what)

新行应该附加到ScrollView的顶部,以便用户可以从nr1开始查看所有行(如图3所示)

图1 - 开始后 - 以前的线路消失了 - 什么错了?视图不可滚动
enter image description here

图2 - 屏幕旋转 - 一切OK,视图可滚动
enter image description here

图3 - 屏幕再次旋转到原始位置 - 一切OK,视图可滚动
enter image description here

XML

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="cz.nielsenadmosphere.androiddemo.NlsCZdemo1.MainActivity">

    <LinearLayout
        android:id="@+id/screen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@color/primary"
        android:orientation="vertical">

        <com.google.android.exoplayer2.ui.SimpleExoPlayerView
            android:id="@+id/player_view"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_gravity="center"
            app:use_controller="false">

        </com.google.android.exoplayer2.ui.SimpleExoPlayerView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/seekBarLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/screen"
        android:layout_centerHorizontal="true"
        android:background="@color/primary_dark"
        android:gravity="center_horizontal">

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="30dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/btns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBarLL"
        android:layout_centerHorizontal="true"
        android:background="@color/primary"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_play"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/txt_play" />

        <Button
            android:id="@+id/btn_pause"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/txt_pause" />

        <Button
            android:id="@+id/btn_end"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/txt_end" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/labels"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btns"
        android:background="@color/primary_light"
        android:orientation="horizontal"
        >

        <TextView
            android:id="@+id/txt_label1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="playHead:"
            android:textColor="@color/secondary_text"
            android:padding="5dp"/>

        <TextView
            android:id="@+id/txt_playtime"
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="0"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>

        <TextView
            android:id="@+id/txt_label2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="status:"
            android:textColor="@color/secondary_text"
            android:padding="5dp"/>

        <TextView
            android:id="@+id/txt_playStatus"
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="null"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>


    </LinearLayout>


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        android:layout_below="@+id/labels"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

            <!-- height must be wrap_content -->
            <TextView
                android:id="@+id/scrollViewText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="3dip"
                android:textSize="12sp"
                >
            </TextView>

    </ScrollView>

</RelativeLayout>

的java.class

    public class displayLogScreen extends Activity{

    static int logPosition = 0;
    static String logTotal = "";
    static String logPart = "";

    private static String TAG = "displayLogScreen";

    static ScrollView scrollView;

    public static void addLog(View view, String what) {


        Log.i(TAG, "addLog what=" + what);

        Calendar logCal = Calendar.getInstance();
        DecimalFormat mFormat= new DecimalFormat("00");
        String logTime =  logCal.get(Calendar.YEAR) + "." + mFormat.format(logCal.get(Calendar.MONTH)+1) + "." + mFormat.format(logCal.get(Calendar.DATE))+ " " + mFormat.format(logCal.get(Calendar.HOUR_OF_DAY)) + ":" + mFormat.format(logCal.get(Calendar.MINUTE)) + ":" + mFormat.format(logCal.get(Calendar.SECOND));

        // just for test
        what = "fooo";

        logPosition ++;
        logPart = "("+ logPosition + ") " + logTime + " " + what + "\n";
        logTotal = logPart + logTotal;

        // add log to ScrollView
        TextView txt_logs = (TextView) view.findViewById(R.id.scrollViewText);
        txt_logs.setText(logTotal);



        // if Error - add Toast
        // if (what.substring(0,5).equals("ERROR"))
        //    Toast.makeText(view.getContext() , what, Toast.LENGTH_SHORT).show();

    }
}

1 个答案:

答案 0 :(得分:0)

您应该指定TextView高度MatchParent和android:textAlignment =“textStart”当您使用单个文本视图附加整个日志时。

这是类似的东西

                <TextView
                android:id="@+id/scrollViewText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="3dip"
                android:textSize="12sp"
                android:textAlignment="textStart"/>

和scrollview的

android:fillViewport="true"