2017-07-06 45 views
0

我正在編寫一個佈局,我必須在垂直線性佈局內填充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); 
    } 

請告訴我,我在哪裏做錯事

+0

不要寬度設置爲您Horizo​​ntalScrollview – tyczj

+0

嘗試把layout_width =「WRAP_CONTENT」你Horizo​​ntalScrollView和LinearLayout中 – ARP

+0

如果使用水平滾動內垂直佈局你會到達什麼?只是嘗試添加textView直接滾動視圖。 – Ibrahim

回答

0

經過多次擊中審判,我想出了正確的解決方案: -

<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); 
    }