2013-02-08 2 views
5

У меня есть LinearLayout в горизонтальной ориентации и 2 ImageView, и я хочу, чтобы ImageView заполнил 50% экрана по ширине, чтобы работать на каждом мобильном телефоне или планшете с разными размерами.Как разместить 50% для ширины

Что-то вроде этого:

+-------------+ 
|_____________|  
|  |  | 
| 50% | 50% | 
|  |  | 
|-------------| 
|    | 
+-------------+ 

Лучшие до сих пор:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="horizontal" > 


     <ImageView 
      android:id="@+id/logo_c" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/logo_animado" /> 

     <ImageView 
      android:id="@+id/logo_t" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/logo_animado2" /> 


</LinearLayout> 
+2

Каждый ImageView должен иметь 'андроида: layout_width = "0dp" и' androdi: layout_weight = "1" ' –

+0

Использовать android: layout_weight = "". – user1744952

+0

Thx @SherifelKhatib Решите мою проблему = DD. –

ответ

9

Напишите следующий код, чтобы сделать это в обоих представлениях внутри LinearLayout.

android:layout_width="0dp" 
layout_weight="1" 
+1

Спасибо Chintan, что было очень полезно = D. –

+0

@Guilherme это мое удовольствие ... :-) –

1

Добавить android:layout_weight="1" в обоих ImageView и сделать ширину, если посмотреть на изображение fill_parent Я думаю, что это решит вашу проблему

Но помните, что это растянет ваше изображение, потому что изображение будет растягиваться в каждом разрешении экрана, так что ваше изображение.

3
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:weightSum="100" 
    android:orientation="horizontal" > 


    <ImageView 
     android:id="@+id/logo_c" 
     android:layout_width="0dp" 
     android:layout_weight="50" 
     android:layout_height="wrap_content" 
     android:src="@drawable/logo_animado" /> 

    <ImageView 
     android:id="@+id/logo_t" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp"  
     android:layout_weight="50" 
     android:src="@drawable/logo_animado2" /> 


</LinearLayout> 
2

Использование Android: weightSum и Android: layout_weight

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:weightSum="2" 
     android:orientation="horizontal" > 


      <ImageView 
       android:id="@+id/logo_c" 
       android:layout_width="0dp" android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:src="@drawable/logo_animado" /> 

      <ImageView 
       android:id="@+id/logo_t" 
       android:layout_width="0dp" android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:src="@drawable/logo_animado2" /> 


    </LinearLayout> 
Смежные вопросы