图像按钮必须放置左右角

时间:2015-07-01 09:01:33

标签: android android-xml

我必须用两个箭头将图像放在左右两侧。但是它靠近中心放置。

我对两个箭头都使用了linearlayout,并将方向设置为水平。我已经发布了下面的代码和截图:

activity_arrow.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/linear"
        android:layout_alignParentTop="true">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/rightArrow"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:layout_weight=".01"
            android:background="@null"
            android:paddingLeft="50dp"
            android:padding="5dp"
            android:src="@drawable/left_arrow"
            />


        <ImageButton
            android:id="@+id/leftArrow"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:layout_weight=".01"
            android:background="@null"
            android:padding="5dp"
            android:src="@drawable/right_arrow" />

    </LinearLayout>

</RelativeLayout>

截图:

enter image description here

6 个答案:

答案 0 :(得分:3)

尝试这一点,它将根据您的要求完美,

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/linear"
        android:layout_alignParentTop="true">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:orientation="horizontal">

        <Button
            android:id="@+id/rightArrow"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:layout_weight=".01"
            android:background="@null"
            android:paddingLeft="50dp"
            android:padding="5dp"
            android:drawableLeft="@drawable/left_arrow"
            />


        <Button
            android:id="@+id/leftArrow"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:layout_weight=".01"
            android:background="@null"
            android:padding="5dp"
             android:drawableRight="@drawable/right_arrow" />

    </LinearLayout>

</RelativeLayout>

<强>截图:

enter image description here 希望这会有所帮助......谢谢

答案 1 :(得分:1)

您应该使用RelativeLayout

android:layout_alignParentBottom将布局移动到屏幕的底部。之后通过使用右对齐并左对齐来对齐RelativeLayout内的图像。使用左移和右移边距。

答案 2 :(得分:1)

尝试这样:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/filter_check2"/>

        <ImageView
            android:id="@+id/filter_check2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/right_arrow"
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <ImageView
            android:id="@+id/filter_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/left_arrow"
            android:layout_gravity="right"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

</RelativeLayout>

<强>输出: -

enter image description here

答案 3 :(得分:0)

你可以试试这个

`

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:gravity="bottom"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/rightArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/leftArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

`

我希望这会有所帮助

答案 4 :(得分:0)

这个怎么样:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/abc_btn_check_material"/>

    <ImageButton
        android:id="@+id/leftArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@null"
        android:src="@drawable/left_arrow"
        />

    <ImageButton
        android:id="@+id/rightArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:src="@drawable/right_arrow"/>

</RelativeLayout>

答案 5 :(得分:0)

不要在视图层次结构顶部使用相对布局。它昂贵,因为它是两阶段布局。 Android UI框架工程师多次不鼓励使用它。以下是Chet Hasse https://medium.com/google-developers/developing-for-android-iii-2efc140167fd

的最新帖子

正如他写的那样

  

重要的是要了解RelativeLayout是昂贵的   解决方案,因为它需要两次测量通过以确保它   已正确处理所有布局关系。

具有权重的LinearLayout也是两阶段布局。只有在没有更好的方法时,我们才应该使用RelativeLayout和LinearLayout。

而不是在根中使用RelativeLayout而使用类似于此

的Framelayout
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/imageViewContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="48dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</FrameLayout>

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

    <Button
        android:id="@+id/rightArrow"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight=".01"
        android:background="@null"
        android:drawableLeft="@drawable/left_arrow"
        android:padding="4dp"
        android:paddingLeft="48dp"
        />


    <Button
        android:id="@+id/leftArrow"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight=".01"
        android:background="@null"
        android:drawableRight="@drawable/right_arrow"
        android:padding="4dp"/>

</LinearLayout>

相关问题