2013-07-05 69 views
22

我想要构建一个像这样的UI http://postimg.org/image/fihidv1rv/。 这里是我的XML代码吧, 为我的设计,我想“效果” &“CAMERA”合并为一个ImageView像在链接 “SHOP”所以会有总共5 ImageView秒,我设置的ID因为他们在链接中命名为Android,如何在relativelayout中设置带有百分比的imageview

问题是,我如何设置高度和宽度的百分比?

效果+ camrea:高度25%,宽度100%

拼贴:高度25%,宽度50%

拉伸:高度25%,宽度50%

照片:高度50% ,宽50%

店:高25%,宽度100%

非常感谢你:'(

<RelativeLayout android:id="@+id/mainContent" android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="horizontal" android:background="#ffffff"> 
    <ImageView 
     android:id="@+id/img_effect+camera" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/a" /> 
    <ImageView 
     android:id="@+id/img_collage" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/img_effect+camera" 
     android:src="@drawable/b" /> 
    <ImageView 
     android:id="@+id/img_draw" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/img_effect+camera" 
     android:layout_toRightOf="@+id/img_collage" 
     android:src="@drawable/c" /> 
    <ImageView 
     android:id="@+id/img_photo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/img_collage" 
     android:layout_below="@+id/img_draw" 
     android:src="@drawable/d" /> 
    <ImageView 
     android:id="@+id/img_shop" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/img_photo" 
     android:src="@drawable/e" /> 
</RelativeLayout> 
+0

你要使用的布局重量 – Sandy09

+0

请检查你的问题。总高度高达125%。将拼贴,绘制和照片在同一行?总比例为150%。 – Ethan

+0

这不是真的125%和150%,因为它们按特殊顺序排列,它显示在此链接http://postimg.org/image/fihidv1rv/ – Kennett

回答

27

您可以考虑使用Android的:在XML layout_weight PARAM布局 http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight

<LinearLayout android:id="@+id/mainContent" android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" android:background="#ffffff"> 
<!-- height 25% --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/img_effect" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:src="@drawable/a" /> 
    <ImageView 
     android:id="@+id/img_camera" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:src="@drawable/a" />  
</LinearLayout> 

<!-- height 50% --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:orientation="horizontal"> 

    <!-- width 50% --> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <!-- height 50%% --> 
     <ImageView 
      android:id="@+id/img_collage" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:src="@drawable/a" /> 

     <!-- height 50%% -->  
     <ImageView 
      android:id="@+id/img_draw" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:src="@drawable/a" />  
    </LinearLayout> 

    <!-- width 50% --> 
    <ImageView 
     android:id="@+id/img_photo" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:src="@drawable/b" /> 
</LinearLayout> 
<!-- height 25%% --> 
<ImageView 
    android:id="@+id/img_shop" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/e" /> 

+1

它的工作原理!我可以问另一个问题吗?例如,我的imageview是10X10,但我的图像是10X20,Android设备将显示图像为5X10,以确保图像正常显示。我如何将它设置为原始图像大小,但忽略太多的部分?也许有点复杂,希望你会明白:) – Kennett

+2

你应该看看ImageView中的android:scaleType参数 - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html。玩所有可用的选项,并选择最适合你的工作。你总是有另一种选择:在传递给ImageView之前裁剪你的图像。 – Ethan

+0

非常感谢!你救了我的朋友的一天 – A7madev

6

它很难,您可以通过代码做一些计算做到这一点。 怎么做:

  • 获取搜索Maincontent高度&宽度,这是你的父视图。
  • GetLayoutparams每个ImageView的,并设置高度&宽度,通过计算百分比高度&宽度

示例代码: (活性)

final View parentView= findViewById(R.id.mainContent); 
final ImageView effect_camera= (ImageView)findViewById(R.id.img_effect+camera); 

effect_camera.post(new Runnable(){ 
    @Override 
    public void run(){ 
    int height=parentView.getHeight(); 
    int width=parentView.getWidth(); 


    RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams)effect_camera.getLayoutParams(); 
    int percentHeight=height*.25; 
    int percentWidth= width*1; 
    lp.height=percentHeight; 
    lp.width=percentWidth; 
    effect_camera.setLayoutParams(lp); 
} 
}); 
+4

请小心:如果将此代码放入onCreate方法中,parentView.getHeight()和.getWidth()将返回0,因为视图尚未绘制。 – chteuchteu

+1

它应该添加在哪里? –

+1

最好在'View.post(Runnable)'方法中调用,这意味着在屏幕上绘制视图时会调用'runnable'。以上例如已经更新。 – vakman

相关问题