以编程方式填充时,线性布局不会水平滚动

时间:2017-07-06 17:31:32

标签: android android-layout horizontalscrollview

我正在进行布局,我必须在垂直线性布局中填充textView programmaticaly。但是我的一些textView内容对于我的屏幕尺寸而言太宽而且它们不可见。这是我的布局

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

    <HorizontalScrollView
        android:layout_width="320px"
        android:layout_height="match_parent">


        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/diff_layout"
            android:layout_width="320px"
            android:layout_height="match_parent"
            android:orientation="vertical">

        </LinearLayout>
    </HorizontalScrollView>
</ScrollView>

这是代码,我以编程方式填充线性布局: -

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.diff_layout);

    String[] splitLines = content.split("\n");
    for(int i=0;i<splitLines.length;i++){
        TextView textView = new TextView(this);
        textView.setSingleLine(true);
        textView.setLines(1);
        //textView.setHorizontallyScrolling(true);
        textView.setText(splitLines[i]);
        //textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ));
        textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        /*textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setHorizontallyScrolling(true);*/
        linearLayout.addView(textView);
    }

请告诉我我在哪里做错了

1 个答案:

答案 0 :(得分:1)

经过多次打击和试验后,我想出了正确的解决方案: -

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <HorizontalScrollView android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/diff_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </LinearLayout>

    </HorizontalScrollView>

</android.support.v4.widget.NestedScrollView>

和我的java片段: -

  

LinearLayout linearLayout =(LinearLayout)   findViewById(R.id.diff_layout);

    String[] splitLines = content.split("\n");
    for(int i=0;i<splitLines.length;i++){
        TextView textView = new TextView(this);
        textView.setText(splitLines[i]);
        textView.setMaxLines(1);
        textView.setMovementMethod(new ScrollingMovementMethod());
        linearLayout.addView(textView);
    }