2016-11-13 74 views
0

我需要将图像视图的顶部与活动的中心对齐。如何将项目的顶部对齐到活动的中心

如果我在android studio中单击并拖动它,我可以轻松地将图像视图的中心链接到活动的中心。但是我需要顶部的图像视图和活动的中心链接。

如何以编程方式执行此操作?我无法设置100dp的边距(例如),因为它不适用于不同的屏幕尺寸。

+0

您可以在一个RelativeLayout中居中放置一个空视图,然后将顶部对齐到该空视图 –

回答

1

我不知道我是否明白你的意思。

我认为你想要将你的活动布局分为两部分,从屏幕中间开始的Imageview。

我的话,你可以用轻松PercentRelativeLayout,这里的代码:

的build.gradle

dependencies { 
    compile 'com.android.support:percent:22.2.0' 
} 

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <ImageView 
      android:src="@color/colorAccent" 
      app:layout_paddingTop="50%" 
      app:layout_heightPercent="50%" 
      app:layout_widthPercent="100%" 
     /> 

     <ImageView 
      android:src="@color/colorPrimary" 
      app:layout_marginTopPercent="50%" 
      app:layout_heightPercent="50%" 
      app:layout_widthPercent="100%" 
     /> 

</android.support.percent.PercentRelativeLayout> 

结果:

visual example

P.S:如果你喜欢,你可以玩布局重量,并得到相同的结果!