如何在屏幕上定位布局

时间:2013-06-17 12:18:28

标签: android android-layout

我需要达到以下结果: 我的RelativeLayout叠加层最初应该看起来像https://dl.dropboxusercontent.com/u/4277073/view1.png。因此,只能看到20%的布局。

按下Button时,我应用Animation以便布局上升并查看以下https://dl.dropboxusercontent.com/u/4277073/view2.png

叠加布局位于父RelativeLayout中,其中包含ImageButtons个数字。

<RelativeLayout
    android:id="@+id/overlay"
    android:layout_width="match_parent"
    android:layout_below="@+id/actionbar"
    android:layout_height="wrap_content"
    android:background="@drawable/backgroundfeedme"
    android:clickable="true"
     >
 -----number of img buttons here -- 
 </RelativeLayout>

我试图设置其初始位置如下

rlOverlay = (RelativeLayout) v.findViewById(R.id.overlay);
ViewTreeObserver vto = rlOverlay.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
      public void onGlobalLayout() {
           WindowManager wm = (WindowManager) MainActivity.appContext.getSystemService(Context.WINDOW_SERVICE);
           Display display = wm.getDefaultDisplay();
           int height = display.getHeight();  // deprecated, my app is for API 2.1+
           rlOverlay.layout(0, (int)(height*0.8), rlOverlay.getMeasuredWidth(), rlOverlay.getMeasuredHeight());
      }
    });

不幸的是,上面的代码不会影响布局的位置。 我也不能使用marginTop,因为它会挤压布局。

那么..如何定位我的布局,以便最初只能看到它的20%?

感谢。

1 个答案:

答案 0 :(得分:0)

我有同样的要求。我做的是放两个相对布局。在第一个布局中取了按钮,在它下面第二个布局,其余的视图和Visiblity =消失了。

来自代码:当我点击按钮时我做了seond布局visiblity =可见和动画它。

这是代码:

    <RelativeLayout
    android:id="@+id/llShopping"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bar_green" >

    <Button
        android:id="@+id/btnShoppingListAddItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bar_green"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:text="Add Item"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/relAddItemChild"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnShoppingListAddItem"
        android:layout_marginBottom="10dp"
        android:visibility="gone" >

        <View
            android:id="@+id/gl"
            android:layout_width="200dp"
            android:layout_height="1dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="1dp"
            android:background="@android:color/white" />

        <EditText
            android:id="@+id/edtItemName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gl"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:hint="Item Name..." />

        <Button
            android:id="@+id/btnSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edtItemName"
            android:layout_centerHorizontal="true"
            android:text="Please pick store" />

        <Button
            android:id="@+id/btnAddToList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnSpinner"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/btn_blue"
            android:padding="5dp"
            android:text="Add To List"
            android:textColor="@android:color/white"
            android:textSize="20sp" />
    </RelativeLayout>
</RelativeLayout>