簡體   English   中英

Android,如何在relativelayout中設置具有百分比的imageview

[英]Android, how to set imageview with percentage in relativelayout

我想建立一個像這樣的http://postimg.cc/image/fihidv1rv/的用戶界面。 下面是它,因為我的設計,我想“效果”和“照相機”作為一個結合自己的XML代碼ImageView像鏈接“SHOP”所以會有總共5 ImageView s和我將id為他們命名在鏈接中

問題是,如何設置高度和寬度百分比?

效果+ 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>

您可以考慮在布局http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight中使用android:layout_weight param

<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" />

在Xml中很難,你可以通過代碼做一些計算。 怎么做:

  • 獲取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);
 }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM