在Android Slide Up面板中以编程方式设置高度

时间:2014-08-09 05:03:52

标签: android

我想做什么 ::

snapshot


我有什么 :: I am referring this project


我在做什么 ::操作栏上的按钮onclick我正在执行滑动操作


发生了什么 ::当滑块出现时,我只想显示一半的屏幕,而不是整个布局


我想要什么 ::

  • 所以我想以编程方式设置高度
  • 然后点击外面的滑动布局必须关闭

我的代码 ::

DemoActivity.java

public class DemoActivity extends Activity {
    private static final String TAG = "DemoActivity";

    private SlidingUpPanelLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        setContentView(R.layout.activity_demo);

        mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
        mLayout.setPanelSlideListener(new PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                Log.i(TAG, "onPanelSlide, offset " + slideOffset);
            }

            @Override
            public void onPanelExpanded(View panel) {
                Log.i(TAG, "onPanelExpanded");

            }

            @Override
            public void onPanelCollapsed(View panel) {
                Log.i(TAG, "onPanelCollapsed");

            }

            @Override
            public void onPanelAnchored(View panel) {
                Log.i(TAG, "onPanelAnchored");
            }

            @Override
            public void onPanelHidden(View panel) {
                Log.i(TAG, "onPanelHidden");
            }
        });

    }



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



        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){

        case R.id.action_cart:
            mLayout.expandPanel();
            //mLayout.hidePanel();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mLayout != null && mLayout.isPanelExpanded()) {
            mLayout.collapsePanel();
        } else {
            super.onBackPressed();
        }
    }
}

activity_demo.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"
    tools:context=".DemoActivity" >

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        sothree:dragView="@+id/dragView"
        sothree:panelHeight="68dp"
        sothree:paralaxOffset="100dp"
        sothree:shadowHeight="4dp" >

        <!-- MAIN CONTENT -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="?android:attr/actionBarSize" >

            <TextView
                android:id="@+id/main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="true"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:gravity="center"
                android:text="Main Content"
                android:textSize="16sp" />
        </LinearLayout>

        <!-- SLIDING LAYOUT -->

        <LinearLayout
            android:id="@+id/dragView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#eeeeee"
            android:clickable="true"
            android:focusable="false"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="68dp"
                android:background="#000000"
                android:orientation="horizontal" >
            </LinearLayout>
        </LinearLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:5)

你可以试试这个......

int height = getWindowManager().getDefaultDisplay().getHeight(); // getting full height of screen
mLayout.setPanelHeight(height/2); // setting half size

或者这个..

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;

mLayout.setPanelHeight(height/2);