对齐控件

时间:2012-07-31 12:38:36

标签: android android-layout css-position relativelayout

我只是在浪费时间,因为我不知道如何正确地做到这一点。基本上,我想要一个自定义输入视图,用户可以用他/她的手指绘制他想要的任何东西,在它下面,我想放置2个(或者更多)按钮,我不想重叠自定义视图的顶部。因为我试图避免硬编码宽度和高度,有没有办法指定按钮应该包装到他们的内容,自定义视图应该占用剩余的高度?由于我希望所有按钮都具有相同的高度,我想将其中一个按钮的高度作为参考应该以某种方式起作用......

这是我目前的布局:

<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=".MainActivity" >

    <com.example.myapp.DrawView
        android:id="@+id/drawView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/save" />

    <Button
        android:id="@+id/buttonQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/query" />

</RelativeLayout>

实际上是否有可能轻松实现我想要的东西,而不必以恶劣的方式破解它?

2 个答案:

答案 0 :(得分:1)

将自定义视图放在Buttons

上方
<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=".MainActivity" >

   <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/save" />

    <Button
        android:id="@+id/buttonQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/query" />

    <com.example.myapp.DrawView
        android:id="@+id/drawView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:layout_above="@id/buttonSave" />

</RelativeLayout>

如果您不确定Buttons相等的高度,则必须将它们包装在另一个布局中(例如LinearLayout),然后将自定义View放在上面包装

答案 1 :(得分:0)

在某些布局上放置按钮:

<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=".MainActivity" >

<com.example.myapp.DrawView
    android:id="@+id/drawView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />


    <RelativeLayout android:id="@+id/button_layout"
              ........  
              android:layout_alignParentBottom="true"
    >
              ....
              your buttons
              .....
    </RelativeLayout>  
</RelativeLayout>