LinearLayout大于屏幕

时间:2014-07-04 07:10:04

标签: android android-layout

我正在尝试创建一个比设备屏幕更宽的水平LinearLayout。它包含一个Button,它将移动布局以在单击时查看屏幕外部的部分。问题是android没有在屏幕外创建部件,所以当我按下按钮时,视图会移动到显示剩余视图的黑屏。

我的代码 -

MainActivity.java

public class MainActivity extends BaseGameActivity implements OnClickListener {

    private Context mContext;
    private int mScreenHeight;
    private int mScreenWidth;
    private LinearLayout mViewContainer;
    private View mTransparentContainer;
    private Button mSliderBtn;
    private View mSlidingView;
    private View mHomeView;
    private int mSlidingWidth;
    private boolean mIsSlidingMenuVisible = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();
        mScreenHeight = UserScreen.getHeight(mContext);
        mScreenWidth = UserScreen.getWidth(mContext);
        mSlidingWidth = (int) (getResources().getDimension(R.dimen.sliding_menu_offset) + 0.5f);

        mViewContainer = new LinearLayout(mContext);
        mViewContainer.setOrientation(LinearLayout.HORIZONTAL);
        mViewContainer.setLayoutParams(new LinearLayout.LayoutParams(mScreenWidth + mSlidingWidth, mScreenHeight));
        setContentView(mViewContainer);

        LinearLayout homeContainer = new LinearLayout(mContext);
        LinearLayout.LayoutParams homeContainerParams = new LinearLayout.LayoutParams(mScreenWidth, LayoutParams.MATCH_PARENT);
        mHomeView = getLayoutInflater().inflate(R.layout.activity_main, homeContainer);
        mHomeView.setBackgroundColor(0x80ffffff);
        mViewContainer.addView(homeContainer, homeContainerParams);

        LinearLayout slidingContainer = new LinearLayout(mContext);
        LinearLayout.LayoutParams slidingContainerParams = new LinearLayout.LayoutParams(mSlidingWidth, LayoutParams.MATCH_PARENT);
        mSlidingView = getLayoutInflater().inflate(R.layout.slider_menu, slidingContainer);
        mViewContainer.addView(slidingContainer, slidingContainerParams);

        initUi();

        // other code
    }

    public void initUi() {
        mSliderBtn = (Button) mHomeView.findViewById(R.id.button_slider);
        mSliderBtn.setOnClickListener(this);

        mTransparentContainer = mHomeView.findViewById(R.id.transparent_container);
        mTransparentContainer.setOnClickListener(this);

        // other code
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button_slider:
            mSliderBtn.setEnabled(false);
            mIsSlidingMenuVisible = true;
            mTransparentContainer.setVisibility(View.VISIBLE);
            mTransparentContainer.bringToFront();
            tableMoveLeft(mViewContainer, mSlidingWidth);
            break;
        case R.id.transparent_container:
            mSliderBtn.setEnabled(true);
            mIsSlidingMenuVisible = false;
            mTransparentContainer.setVisibility(View.GONE);
            tableMoveRight(mViewContainer, mSlidingWidth);
            break;
        // other cases
        }
    }

    // Animation Functions

    public void tableMoveLeft(final View container, final float newPos){
        ObjectAnimator moveLeft = ObjectAnimator.ofFloat(container, "translationX", 0, -newPos);
        moveLeft.setDuration(300);
        moveLeft.start();
    }

    public void tableMoveRight(final View container, final float newPos){
        ObjectAnimator moveRight = ObjectAnimator.ofFloat(container, "translationX", -newPos, 0);
        moveRight.setDuration(300);
        moveRight.start();
    }

    // other code

}

activity_main.xml中

<RelativeLayout 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:background="@drawable/bg_menu"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- other code -->

    <Button
        android:id="@+id/button_slider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dip"
        android:layout_marginTop="30dip"
        android:background="@drawable/move" />

    <View
        android:id="@+id/transparent_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80800000"
        android:visibility="gone" />

</RelativeLayout>

slider_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/image_info_bg_popup"
    android:orientation="vertical" >

    <!-- Other Code -->

</LinearLayout>

单击按钮时的输出

Output

使用ScrollView不是解决方案,因为我不希望用户自由滚动视图,并且主布局中还有一个ViewPager与之冲突。请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

Android通过引入一个名为Navigation Drawer的新概念正式推出了滑动面板菜单。大多数情况下,滑动菜单(Navigation Drawer)将被隐藏,可以通过从左到右滑动屏幕或点击操作栏上的应用程序图标来显示。

例如,以下布局使用带有两个子视图的DrawerLayoutFrameLayout包含主要内容(在运行时由片段填充),以及ListView导航抽屉。

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

有关NavigationDrawer的其他信息,请查看以下链接:

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ http://developer.android.com/training/implementing-navigation/nav-drawer.html

答案 1 :(得分:0)

问题是大小大于屏幕大小的布局被设置为活动的根容器。我不知道由此引起的问题是什么,但要解决它 -

@Override
public void onCreate(Bundle savedInstanceState) {

    super(savedInstanceState);

    LinearLayout rootContainer = new LinearLayout(mContext);
    setContentView(rootContainer);

    // other code

    rootContainer.addView(mViewContainer);
}

欢迎任何解释为什么根视图大于屏幕尺寸会产生问题:)