2012-03-23 84 views
6

我嘗試使用下面的代碼來滾動我的TextView:的TextView沒有滾動順利

TextView text = (TextView) findViewById(R.id.textView12); 
     text.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     int height = getResources().getDrawable(R.drawable.pic_ground_pranic).getIntrinsicHeight(); 
     text.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, height)); 
     text.setText("As part of an Android App I am building a button set.For performance reasons and to keep the code simpler, the Android system uses pixels as the standard unit for expressing dimension or coordinate values. That means that the dimensions of a view are always expressed in the code using pixels, but always based on the current screen density. For instance, if myView.getWidth() returns 10, the view is 10 pixels wide on the current screen, but on a device with a higher density screen, the value returned might be 15. If you use pixel values in your application code to work with bitmaps that are not pre-scaled for the current screen density, you might need to scale the pixel values that you use in your code If you need to control exactly how your application will look on various screen configurations, adjust your layouts and bitmap drawables in configuration-specific resource directories. For example, consider an icon that you want to display on medium and high density screens. Simply create your icon at two different sizes (for instance 100x100 for medium density and 150x150 for high density) and put the two variations in the appropriate directories, using the proper qualifiers: For instance, suppose a device has a WVGA high-density screen, which is 480x800 and about the same size as a traditional HVGA screen, but it's running an application that has disabled pre-scaling. In this case, the system will lie to the application when it queries for screen dimensions, and report 320x533 (the approximate mdpi translation for the screen density). Then, when the application does drawing operations, such as invalidating the rectangle from (10,10) to (100, 100), the system transforms the coordinates by scaling them the appropriate amount, and actually invalidate the region (15,15) to (150, 150). This discrepancy may cause unexpected behavior if your application directly manipulates the scaled bitmap, but this is considered a reasonable trade-off to keep the performance of applications as good as possible. If you encounter this situation, read Based on the density of the current screen, the system uses any size- or density-specific resources from your application and displays them without scaling. If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density. The system assumes that default resources (those from a directory without configuration qualifiers) are designed for the baseline screen density (mdpi), unless they are loaded from a density-specific resource directory. Pre-scaling is, thus, what the system does when resizing a bitmap to the appropriate size for the current screen density. to match the un-scaled bitmap source. The buttons are part of a nested set of LinearLayouts.The question then is: How to resize the layout.I have tried several suggested techniques, and none come close to working. Here is a subset of the XML that builds the button set:"); 

所有工作正常,但我的文本視圖不能順暢地滾動。我以爲我必須把我的Textview放入Scrollview中,但這也不起作用。

這是否發生,因爲我正在調整我的textview?

請爲此問題提供任何解決方案。我已經花了很多時間。

我的XML佈局文件是:提前

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/include1" 
     android:layout_centerHorizontal="true" 
     android:background="@android:color/white" 
     android:scrollbarStyle="insideInset" > 

     <TableLayout 
      android:id="@+id/tableLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:padding="5dp" > 

      <TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <RelativeLayout 
        android:id="@+id/relativeLayout2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" > 

        <TextView 
         android:id="@+id/textView2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_marginLeft="5dp" 
         android:layout_marginTop="14dp" 
         android:text="Text 1" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView3" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignBottom="@+id/textView2" 
         android:layout_marginLeft="25dp" 
         android:layout_toRightOf="@+id/textView2" 
         android:text="Text 2" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView4" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignBottom="@+id/textView3" 
         android:layout_marginLeft="25dp" 
         android:layout_toRightOf="@+id/textView3" 
         android:text="Text 3" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView5" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignTop="@+id/textView4" 
         android:layout_marginLeft="25dp" 
         android:layout_toRightOf="@+id/textView4" 
         android:text="Text 4" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView6" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_below="@+id/textView2" 
         android:layout_marginLeft="5dp" 
         android:layout_marginTop="3dp" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView7" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@+id/textView3" 
         android:layout_below="@+id/textView3" 
         android:layout_marginTop="3dp" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView8" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@+id/textView4" 
         android:layout_below="@+id/textView4" 
         android:layout_marginTop="3dp" 
         android:textColor="@android:color/black" /> 

        <TextView 
         android:id="@+id/textView9" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@+id/textView5" 
         android:layout_below="@+id/textView5" 
         android:layout_marginTop="3dp" 
         android:textColor="@android:color/black" /> 
       </RelativeLayout> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <RelativeLayout 
        android:id="@+id/relativeLayout3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" > 

        <SeekBar 
         android:id="@+id/seekBar1" 
         android:layout_width="150dp" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:soundEffectsEnabled="true" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_centerVertical="true" 
         android:text="Volume 1" 
         android:textColor="@android:color/black" /> 
       </RelativeLayout> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <RelativeLayout 
        android:id="@+id/relativeLayout4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" > 

        <SeekBar 
         android:id="@+id/seekBar2" 
         android:layout_width="150dp" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:soundEffectsEnabled="true" /> 

        <TextView 
         android:id="@+id/textView11" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_centerVertical="true" 
         android:text="Volume 2" 
         android:textColor="@android:color/black" /> 
       </RelativeLayout> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:baselineAligned="false" > 

       <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" 
        android:orientation="vertical" > 

        <Button 
         android:id="@+id/button1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="right" 
         android:text="Save Volumes" /> 
       </LinearLayout> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

       <FrameLayout 
        android:id="@+id/frameLayout3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" 
        android:orientation="vertical" > 

        <TextView 
         android:id="@+id/textView12" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_gravity="center_vertical|center_horizontal" 
         android:gravity="top|left" 
         android:scrollbars="vertical" 
         android:visibility="visible" /> 
       </FrameLayout> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow8" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <FrameLayout 
        android:id="@+id/frameLayout2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" > 

        <ImageView 
         android:id="@+id/imageView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="right|center" /> 

        <TextView 
         android:id="@+id/textView13" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="right|center" 
         android:layout_marginRight="30dp" 
         android:clickable="true" 
         android:text="Detailed Instruction" /> 
       </FrameLayout> 
      </TableRow> 
     </TableLayout> 
    </ScrollView> 

</RelativeLayout> 

謝謝!

+0

問題解決了嗎?請按我的要求查看我的答案。並接受它,如果它的工作。 – 2012-03-26 05:06:35

回答

2

問題是ScrollView裏面滾動查看

當你把ScrollView放在ScrollView裏面的時候,只是感到困惑了哪個滾動視圖被觸摸了。所以有時它無法提供觸摸事件。

找了一段時間後,我找到了一個鏈接或博客,將解壓縮此問題,也有解決方案。

看看this的博客。這正是你一直在尋找的,我相信它會解決你的問題。

現在時間享用;)

+0

我試過了,現在我的問題解決了。感謝這個有用的答案。 – Pari 2012-05-18 11:37:59

0

首先,不需要設置Layoutparams,因爲您已將它放入xml佈局。

如果你有這麼長的字符串,那麼不要把它直接放在java代碼上。而不是把它放到資源目錄。

轉到值,並打開strings.xml文件並定義您的字符串。

見下文:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, HelperStackOverflowActivity!</string> 
    <string name="app_name">HelperStackOverflow</string> 
    <string name="String1">"PUT YOUR STRING HERE" </string> 

</resources> 

這樣做,設置在Java代碼中的字符串,只要你想後。 像:

text = (TextView)findViewById(R.id.textView12); 
    text.setText(getResources().getString(R.string.String1)); 

它像正常滾動一樣滾動很好。

希望它能幫助你。

享受。 :)

+0

首先抱歉讓我回應,我正在做你提到的所有這些,但這無法幫助。 TextView不會作爲我的異常滾動。我正在這樣做 'TextView textView =(TextView)findViewById(R.id.textView12); \t \t TextView text =(TextView)findViewById(R.id.textView12); text.setMovementMethod(ScrollingMovementMethod.getInstance()); textView.setText(getResources()。getString(R.string.myString));' 我在這裏工作爲什麼TextView不滾動? – Pari 2012-03-28 12:52:44

+0

刪除你的所有行,只需添加這一行和try.TextView text =(TextView)findViewById(R.id.textView12); text.setText(getResources()的getString(R.string.myString)); – 2012-03-28 13:37:59

+0

如果我這樣做,你說TextView hight自動增加根據像「包裝內容」的文字,如果我將高度設置爲「193px」,那麼它不滾動。我認爲不使用ScrollView textview不可滾動。 – Pari 2012-03-29 15:17:27

3

只有在限制其高度的TextView滾動。簡單的規則是當文本超過可用空間,然後textview開始滾動。 其中一種方法是通過指定從某個圖像大小獲得的固定高度來追蹤。另一種方法是設置最大行數。

TextView text = (TextView) findViewById(R.id.textView12); 
text.setMaxLines(5); 
text.setMovementMethod(ScrollingMovementMethod.getInstance()); 

你甚至可以使它更具動感。只需通過計算行數。線的

總數=(以像素爲單位的圖像高度)/(在像素文本字體大小)

+1

平滑了我的textview。謝謝。 – jasonflaherty 2012-11-06 22:31:10

3

在我的情況我只需要把我的TextView滾動型的內部。滾動變得非常流暢,用戶每次都過得很開心!

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    > 
    <TextView 
     android:id="@+id/textview_about" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="true" 
     android:textSize="20dp" /> 
</ScrollView>