日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ScrollView’s handy trick

發布時間:2024/4/17 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ScrollView’s handy trick 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ScrollView is one of Android’s most commonly used widget and is also one of the easiest to use. When something is too big to fit on screen, drop it inside a ScrollView and you’re done. You can’t even get it wrong since a ScrollView accepts only one child at a time. There is, however, one use case a bit trickier to get right; unless you’ve carefully read the documentation.

Let’s imagine that your application needs to display a piece of text and a couple of buttons. The length of the text can vary and be longer or shorter than the screen. You want to put the text inside a scroll view and you want the buttons to scroll along with the text, probably to encourage the user to read the text before clicking any of the button. Depending on the length of the text, your application would look like one of the following screenshots:

In attempt to achieve this effect, I have seen several Android developers try to set the height of the view inside the scroll view to fill_parent. Doing so does not work and leads to the following result:

To understand this result, you must remember that android:layout_height=”fill_parent” means “set the height to the height of the parent.” This is obviously not what you want when using a ScrollView. After all, the ScrollView would become useless if its content was always as tall as itself. To work around this, you need to use the ScrollView attribute called android:fillViewport. When set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.

The XML I wrote to create the correct version of this example can be found below. In line 32, I’ve set the android:layout_weight of the TextView to 1.0. By doing so I am forcing the text to use the available empty space when it is shorter than the ScrollView. This can only work when android:fillViewport=”true” is set on the scroll view.

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
??? android:id="@+id/scroller"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? android:fillViewport="true" >
??? <LinearLayout
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:orientation="vertical">
??????? <TextView
??????????? android:layout_width="fill_parent"
??????????? android:layout_height="wrap_content"
??????????? android:paddingLeft="6dip"
??????????? android:paddingRight="6dip"
??????????? android:paddingTop="6dip"
??????????? android:textAppearance="?android:attr/textAppearanceMedium"
??????????? android:text="Welcome to My Application" />

??????? <View
??????????? android:layout_width="fill_parent"
??????????? android:layout_height="1dip"
??????????? android:background="#ff106510"
??????????? android:layout_marginLeft="6dip"
??????????? android:layout_marginRight="6dip"
??????????? android:layout_marginTop="6dip"
??????????? android:layout_marginBottom="12dip" />

??????? <TextView
??????????? android:layout_width="fill_parent"
??????????? android:layout_height="wrap_content"
??????????? android:layout_weight="1.0"

??????????? android:paddingLeft="6dip"
??????????? android:paddingRight="6dip"
??????????? android:paddingBottom="6dip"

??????????? android:text="@string/hello" />

??????? <LinearLayout
??????????? android:layout_width="fill_parent"
??????????? android:layout_height="wrap_content"

??????????? android:background="@android:drawable/bottom_bar"
??????????? android:gravity="center_vertical">
??????????? <Button
??????????????? android:layout_width="0dip"
??????????????? android:layout_weight="1.0"
??????????????? android:layout_height="wrap_content"

??????????????? android:text="Accept" />
??????????? <Button
??????????????? android:layout_width="0dip"
??????????????? android:layout_weight="1.0"
??????????????? android:layout_height="wrap_content"

??????????????? android:text="Refuse" />
??????? </LinearLayout>
??? </LinearLayout>
</ScrollView>

Last but not least, I realized while writing this that the documentation of ScrollView does not mention the fillViewport XML attribute, even though it shows the equivalent Java API, setFillViewport(). This is a stupid mistake on my part that I will fix next week for a future release of Android. In the meantime, please accept my apologies :) just fixed.

轉載于:https://www.cnblogs.com/RayLee/archive/2010/09/07/1820916.html

總結

以上是生活随笔為你收集整理的ScrollView’s handy trick的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。