2013-09-05 2 views
0

각 수평 LinearLayout의 높이를 추측하지 않고 이미지를 터치하는 방법이 있습니까? 당신은 내가이 효과를 얻기 위해 144dp에있는 LinearLayout의 높이를 추측했다 나의 XML에서 볼 수 있듯이Android :이 레이아웃을 얻는 방법

Can i do this without guessing the layout heights?

, 다양한 화면 크기에 일하는 것이 더 나은 방법은 무엇입니까?

<LinearLayout 
     android:id="@+id/LinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/LinearLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" > 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView02" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" 
      android:layout_gravity="fill_horizontal" > 

      <ImageView 
       android:id="@+id/imageView03" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 

      <ImageView 
       android:id="@+id/imageView04" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout03" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" 
      android:layout_gravity="fill_horizontal" > 

      <ImageView 
       android:id="@+id/ImageView05" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView06" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 
    </LinearLayout> 

가능한 경우 XML로 처리하는 것을 선호합니다. 이미지는 정사각형이므로 너비를 채우고 여전히 비례해야합니다. 나는 fitXY로 거의 그것을 할 수있다. 그러나 그것은 단지 그들의 폭이 아니라 그들의 높이를 뻗는다.

+1

어떻게 선형 레이아웃의 높이를 추측하고 있습니까? 선형 레이아웃이 이미지의 높이에 따라 달라지는 높이를 추측하고 있다는 것을 의미합니까? 각 선형 선형 레이아웃을 수직 선형 레이아웃에 넣고 원하는 것을 얻을 수 있습니까? –

+0

이미지가 LinearLayout을 완벽하게 채울 때까지 LinearLayout01의 높이를 늘 렸습니다 (선형 레이아웃이 정사각형이되었습니다) – Edd

+0

@cskoala LinearLayouts를 사용하여 수직선을 만드는 것이 좋습니다. 2 개의 수직의 LinearLayouts의 사이에 약간의 간격을 두어 화면을 수직 방향으로 전부 칠합니다. 아마 scaleXY – Edd

답변

-1

완벽하게 사각형이 있습니다 (그 상당히 눈에 띄지 않지만) 내가 사용하여이 문제를 해결하기 위해 관리 2 수직 LinearLayout는, 대신에 3 수평 것들하고 나머지 격차를 채우기 위해 ScaleType=fitXY를 사용하는이야이 나를 떠나 : 코드에서

Final Layout

:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:weightSum="3" > 

     <ImageView 
      android:id="@+id/ImageView03" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

     <ImageView 
      android:id="@+id/ImageView01" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

     <ImageView 
      android:id="@+id/ImageView02" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/LinearLayout02" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:weightSum="3" > 

     <ImageView 
      android:id="@+id/ImageView04" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

     <ImageView 
      android:id="@+id/imageView03" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

     <ImageView 
      android:id="@+id/imageView04" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

    </LinearLayout> 

</LinearLayout> 
2

많은 LinearLayouts 대신 GridView를 사용해야합니다. 당신이 당신의 코드를 유지하려면

이 업데이트 : 다른 "레이아웃 래퍼는"이 있어야

android:layout_height="match_parent" 

:

android:layout_height="wrap_content" 
android:orientation="horizontal" 

메인 레이아웃이 속성을 가져야한다 귀하의 imageView는 다음을 가져야합니다 :

android:layout_height="wrap_content" 

정사각형 이미지로만 작업하고 있습니까?

내가 코드를 업데이트하고 그것을 시도하지 않은,하지만 작동합니다 : 이미지 하다니

<LinearLayout 
     android:id="@+id/LinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/LinearLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView02" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/imageView03" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 

      <ImageView 
       android:id="@+id/imageView04" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout03" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="fill_horizontal" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/ImageView05" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView06" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 
    </LinearLayout> 
+0

'layout_weight'를 사용하는 경우' 'layout_height' 또는'layout_width' 중 어느 것을 원하는지에 따라 '0dp'를 사용하십시오. – Eluvatar

+0

내 모든 이미지가 정사각형입니다. 정사각형 이미지 만 사용하면됩니다. – Edd

+0

코드가 작동하지 않습니다. (수직으로 만져도 큰 간격이 수평으로 다시 가도록합니다. – Edd

관련 문제