Подтвердить что ты не робот

Android, как установить изображение с процентом в relativelayout

Я хочу создать UI, подобный http://postimg.org/image/fihidv1rv/. Вот мой xml-код для него, для моего дизайна я хочу, чтобы "EFFECT" и "CAMERA" объединялись как один ImageView как "SHOP" в ссылке поэтому будет всего 5 ImageView s, и я устанавливаю идентификатор, как они названы в ссылке

проблема в том, как установить высоту и ширину с процентом?

эффект + camrea: высота 25%, ширина 100%

коллаж: высота 25%, ширина 50%

draw: высота 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>
4b9b3361

Ответ 1

Вы можете рассмотреть использование параметра android: layout_weight в макете 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" />

Ответ 2

Сложно это сделать в Xml, вы можете сделать это, выполнив некоторые вычисления. Как это сделать:

  • Получить высоту и ширину mainContent, который является вашим родительским представлением.
  • GetLayoutparams каждого изображения и задайте высоту и ширину, вычислив процентную высоту и ширину.

Пример кода: (в действии)

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);
 }
});