簡體   English   中英

將視圖置於滾動視圖上方

[英]Place view above scrollview

我的Android應用程序中有一個scrollview,它支持過度滾動並具有不錯的彈跳效果。 我想做的是添加一個最初對用戶隱藏的視圖,但是如果他們向上滾動到初始視圖之外,那么他們可以看到它。 我怎樣才能做到這一點? 是否可以僅使用xml來做到這一點?

您可以將初始視圖和其他視圖放置在LinearLayout ,並且在創建滾動視圖時,可以立即向下滾動到初始視圖。 您可以使用xml屬性android:scrollY設置初始滾動偏移量。

通過代碼,您絕對可以實現這一目標。 在此示例代碼中,我在srollview中有15個按鈕。 並隱藏第一個按鈕以進行初始顯示。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.sview);

    hscrollViewMain.post(new Runnable() {
       public void run() {

            Button bt2 = (Button)findViewById(R.id.button2);
            int nY_Pos = bt2.getTop(); 
            // scroll to top of bt2
            hscrollViewMain.scrollTo(0,nY_Pos);
        }
    }); 
}

main.xml中

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

<LinearLayout

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="fill_parent"
         android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:text="Button 2" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button 3" />
                .
                .
                .
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button 15" />
    </LinearLayout>

    </ScrollView>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM