Android 安卓:重力能做到吗?
搜索了一整天,我想找到了一个糟糕的解决方案。Android 安卓:重力能做到吗?,android,android-relativelayout,Android,Android Relativelayout,搜索了一整天,我想找到了一个糟糕的解决方案。 我想把三个ImageButton放在屏幕的右边。 不居中,但刚好在中心位置上方 通过下面的代码,我得到了想要的结果,但是, 如果用户有一个更大的屏幕,他们将不会有这个位置,对吗 我一整天都在尝试android:gravity,我能做的就是 将三个按钮很好地居中放置 我应该使用什么使三个按钮始终保持在 它们在贝洛夫的图片上。 我在hdpi、mdpi、xhdpi文件夹中有3种不同大小的按钮图像 <RelativeLayout
我想把三个ImageButton放在屏幕的右边。
不居中,但刚好在中心位置上方 通过下面的代码,我得到了想要的结果,但是,
如果用户有一个更大的屏幕,他们将不会有这个位置,对吗 我一整天都在尝试android:gravity,我能做的就是 将三个按钮很好地居中放置 我应该使用什么使三个按钮始终保持在 它们在贝洛夫的图片上。 我在hdpi、mdpi、xhdpi文件夹中有3种不同大小的按钮图像
<RelativeLayout
android:id="@+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="A"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="B"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_A"
/>
<ImageButton
android:id="@+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="C"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_B"
/>
</RelativeLayout>
右边有三个按钮的照片,当然还有我女儿 您可以在RelativeLayout中添加LinearLayout,然后在LinearLayout上使用以下属性:
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
编辑:好的,我已经做了一些测试,找到了一种不需要使用编程样式就可以实现的方法,下面是xml:
<RelativeLayout
android:id="@+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="80dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ImageButton
android:id="@+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
<ImageButton
android:id="@+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
<ImageButton
android:id="@+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
</LinearLayout>
</RelativeLayout>
您可以在RelativeLayout中添加LinearLayout,然后在LinearLayout上使用以下属性:
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
编辑:好的,我已经做了一些测试,找到了一种不需要使用编程样式就可以实现的方法,下面是xml:
<RelativeLayout
android:id="@+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="80dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ImageButton
android:id="@+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
<ImageButton
android:id="@+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
<ImageButton
android:id="@+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:src="@drawable/icon"
android:background="@android:color/transparent"
/>
</LinearLayout>
</RelativeLayout>
一个选项是将所有三个按钮放在线性布局中(为了简单起见),然后以编程方式设置此容器的布局
您可以使用
getResources().getDisplayMetrics().heightPizels
查看屏幕大小,然后相应地设置顶部边距。一个选项是将所有三个按钮放在一个线性布局中(为了简单起见),然后以编程方式设置此容器的布局
您可以使用
getResources().getDisplayMetrics().heightPizels
查看屏幕大小,然后相应地设置顶部边距。好的,那么您最好按照Zack所说的做,并通过编程设置边距。我想这几乎就是我已经拥有的。如果应用程序在更大的屏幕上运行,android:layout_marginBottom=“80dip”将出现问题。我需要一个程序计算,无论屏幕大小如何,都能将一行按钮始终放在同一个位置OK,那么你最好按照Zack所说的做,并通过编程设置边距。我想这几乎就是我已经拥有的。如果应用程序在更大的屏幕上运行,android:layout_marginBottom=“80dip”将出现问题。我需要一个程序计算,无论屏幕大小如何,都能将按钮行始终放在同一位置。我知道如何使用rightRelativeLayout.addView。要添加动态创建的视图,但问题是我现在是否应该使用rightRelativeLayout.layout(l、t、r、b)。我不添加视图,只更改布局高度。l=左,t=顶,r=右。@Erik您没有更改布局高度。你需要做的就是更改上边距,对吗?我知道如何使用rightRelativeLayout.addView。要添加动态创建的视图,但问题是我现在是否应该使用rightRelativeLayout.layout(l、t、r、b)。我不添加视图,只更改布局高度。l=左,t=顶,r=右。@Erik您没有更改布局高度。你所需要做的就是改变上边距,对吗?可能重复的可能重复的