滚动型有两种观点,第一种观点填充屏幕 [英] ScrollView with two views, first view filling screen

查看:143
本文介绍了滚动型有两种观点,第一种观点填充屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个包含两个意见的LinearLayout滚动型。第一视图应该占据全屏,与第二视图最初关闭屏幕(但它可以滚动到)。这是可以实现的?

I would like to have a ScrollView with a LinearLayout containing two views. The first view should occupy the full screen, with the second view initially off the screen (but it can be scrolled to). Is this achievable?

推荐答案

使用code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/f1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#468432" >

        </FrameLayout>

        <FrameLayout
            android:id="@+id/f2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ff8222" >

        </FrameLayout>

    </LinearLayout>

</ScrollView>

和你的活动GOSE像下面

and your activity gose like following

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout.LayoutParams; 
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

    public class MainActivity extends Activity {

        LinearLayout layout ;
        FrameLayout f1,f2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            layout = (LinearLayout)findViewById(R.id.linlayout);
            f1=(FrameLayout) findViewById(R.id.f1);
            f2=(FrameLayout) findViewById(R.id.f2);
            ViewTreeObserver vto = layout.getViewTreeObserver(); 
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
                @Override 
                public void onGlobalLayout() { 
                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                    int width  = layout.getMeasuredWidth();
                    int height = layout.getMeasuredHeight(); 

                    f1.setLayoutParams(new LayoutParams(width, height));
                    f2.setLayoutParams(new LayoutParams(width, height));
                } 
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

这篇关于滚动型有两种观点,第一种观点填充屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆