2016-06-28 3 views
1

다음은 내 xml 코드입니다. 안에 내 LinearLayout. 나는 두 개의 자식 뷰를 가지고 있는데, 이는 weightSum을 사용하여 정렬됩니다. weightSum으로 인해 너비가 올바르게 정렬 된 ImageView이 있지만 높이가 문제입니다. 높이가 너비와 같은 치수가되기를 원합니다. static dp 값을 사용하지 않고 해결책이 있습니까?Android 너비와 높이가 같음보기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:paddingBottom="5dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp"> 

    <LinearLayout 
     android:background="@drawable/background_gradien_yellow" 
     android:weightSum="10" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

      <ImageView 
       android:layout_weight="6" 
       android:id="@+id/startbtn" 
       android:textStyle="bold" 
       android:alpha="0.8" 
       android:textColor="#fff" 
       android:text="@string/start" 
       android:layout_centerInParent="true" 
       android:background="@drawable/background_gradient_circular" 
       android:layout_width="match_parent" 
       android:layout_height="150dp"/> 


     <LinearLayout 
      android:layout_marginTop="20dp" 
      android:layout_marginBottom="20dp" 
      android:orientation="vertical" 
      android:layout_weight="3" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <TextView 
       android:textColor="@color/White" 
       android:id="@+id/tv_challange_title" 
       android:textSize="20sp" 
       android:text="Early-Bird Challange" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

      <TextView 
       android:id="@+id/tv_challange_desc" 
       android:alpha="0.5" 
       android:textColor="@color/White" 
       android:textSize="15sp" 
       android:layout_marginTop="5dp" 
       android:text="Take 100 steps five times within.. " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 


     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 

답변

3

사용자 정의 ImageView 확인 후 width로서의 height 동일하게 설정. check this answer

+0

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

XML 코드. 사용자 정의 ImageView 사용 –

+0

imageView scaleType을 "center"로 설정하십시오. –

5

당신의 활동의 자바 코드로 할 수 있다고 생각합니다. 이와 같은

뭔가 :

android.view.ViewGroup.LayoutParams mParams = imageView.getLayoutParams(); 
mParams.height = imageView.getWidth(); 
imageView.setLayoutParams(mParams); 
+0

xml 코드 –

+1

에 어떤 가능성이 있습니까? 내 마음에 들었던 것입니다. 가중치는 단일 방향에만 포함될 수 있기 때문에 xml 코드에서 그렇게 할 수 없습니다. 이 코드는 올바르게 작동하면 작동합니다. 너비가 0 인 경우이 메소드를 내부 실행 가능 메소드 또는 getViewTreeObserver에 추가하십시오. –

0

당신이 시도 할 수는 또한

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:paddingBottom="5dp"`enter code here` 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp"> 

    <LinearLayout 
     android:background="@color/colorPrimary" 
     android:weightSum="10" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:layout_weight="6" 
      android:id="@+id/startbtn" 
      android:textStyle="bold" 
      android:alpha="0.8" 
      android:textColor="#fff" 
      android:text="start" 
      android:layout_centerInParent="true" 
      android:background="@drawable/common_ic_googleplayservices" 
      android:layout_width="match_parent" 
      android:layout_height="150dp"/> 


     <LinearLayout 
      android:layout_marginTop="20dp" 
      android:layout_marginBottom="20dp" 
      android:orientation="vertical" 
      android:layout_weight="3" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="2"> 


      <TextView 
       android:textColor="#fff" 
       android:id="@+id/tv_challange_title" 
       android:textSize="20sp" 
       android:text="Early-Bird Challange" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 

      <TextView 
       android:id="@+id/tv_challange_desc" 
       android:alpha="0.5" 
       android:textColor="#000" 
       android:textSize="15sp" 
       android:layout_marginTop="5dp" 
       android:text="Take 100 steps five times within.. " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 


     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 
0

은 폭과 높이 (더 나은 .9patch)에 동일한 크기의 이미지를 사용하는 내부 선형 레이아웃에 가중치를 부여

 <ImageView 
     android:layout_weight="6" 
     android:id="@+id/startbtn" 
     android:textStyle="bold" 
     android:alpha="0.8" 
     android:textColor="#fff" 
     android:text="start" 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 

     android:src="@drawable/background_gradient_circular" 
     android:scaleType="fitCenter" 
     android:layout_height="match_parent" /> 
1

당신은 CE로 이미지 뷰의 scaleType을 설정 nterInCrop

android:scaleType="centerInCrop"

+0

어떻게이 질문과 관련이 있습니까? –

+0

그는 단지 ImageView의 높이와 너비를 – PriyankaChauhan

+0

으로 설정하고 싶지만이 대답은 imageView의 높이와 너비의 크기 조정에 관련이 없습니다. –

2
커스텀 이미지 뷰를 사용하여 발견

용액. 최고의 솔루션입니다. 높이는 사용자 정의 ImageView 클래스의 너비와 동일하게 설정합니다.

자바 코드 : 그것의 내 부모 뷰를 스트레칭

  <com.mypakage.utils.SquareImageView 
      android:background="@drawable/fitbit" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
+0

그건 내 친구가 환호하는 데 매우 유용합니다! – oguzhan

관련 문제