2016-04-19 3 views
1

Возможно ли, что представление должно сделать его таким же, как ширина?Установка высоты высоты в зависимости от ее ширины в android в макете xml

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

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/googleLoginBtn" 
      android:orientation="horizontal" 
      android:layout_centerVertical="true"> 

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


       <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="<height should be equal to the width>" 
        app:srcCompat="@drawable/simple" 
        tools:ignore="MissingPrefix"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Snappy" 
        android:textColor="#FF4081" 
        android:textSize="30dp" 
        android:singleLine="true" 
        android:gravity="center" 
        android:layout_margin="10dp"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions." 
        android:layout_margin="10dp"/> 

      </LinearLayout> 


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

       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:srcCompat="@drawable/simple" 
        tools:ignore="MissingPrefix"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Smart" 
        android:textColor="#FF4081" 
        android:textSize="30dp" 
        android:gravity="center" 
        android:layout_margin="10dp"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions." 
        android:layout_margin="10dp"/> 

      </LinearLayout> 
</LinearLayout> 
+1

Возможный дубликат [делая вид сетки элементов квадратных] (http://stackoverflow.com/questions/ 6557516/making-grid-view-items-square) – petey

ответ

2

Это не возможно полностью в xml без использования пользовательского ImageView. Это было бы решение:

public class SquareImageView extends ImageView { 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int width = getMeasuredWidth(); 
    setMeasuredDimension(width, width); 
    } 
} 

Однако если ваше изображение уже Square вы можете использовать обычный ImageView:

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scaleType="fitCenter" 
    android:adjustViewBounds="true" /> 

Третье решение будет установка ширины и высоты прямо в ваш код:

LayoutParams params = imageView.getLayoutParams(); 
params.height = params.width ; 
imageView.setLayoutParams(params); 
+0

Я использую векторное изображение, поэтому второе решение не подходит. – dearvivekkumar

+0

Один простой вопрос: вызывается функция 'onMeasure()' каждый раз, когда размер изменяется? – dearvivekkumar

+0

'onMeasure' вызывается каждый раз, когда родительский вид должен изменить свой макет. Так да. Его даже можно назвать несколько раз. – dipdipdip

2

Для этого вы должны использовать Percent support library. Вы можете либо заменить LinearLayoutImageView находится внутри с PercentRelativeLayout и имеют другие взгляды android:layout_below или обернуть ImageView в PercentFrameLayout.

Я предполагаю, что вы уже определили пространство имен приложений в этом файле как xmlns:app="http://schemas.android.com/apk/res-auto", так как я вижу, что вы используете пространство имен.

После того, как вы включили соответствующую библиотеку поддержки, то PercentFrameLayout подход будет выглядеть примерно так:

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

      <android.support.percent.PercentFrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_width="fill_parent" 
       app:layout_aspectRatio="100%" 
       app:srcCompat="@drawable/simple" 
       tools:ignore="MissingPrefix"/> 
      </android.support.percent.PercentFrameLayout> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Snappy" 
       android:textColor="#FF4081" 
       android:textSize="30dp" 
       android:singleLine="true" 
       android:gravity="center" 
       android:layout_margin="10dp"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions." 
       android:layout_margin="10dp"/> 

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