2013-05-25 6 views
18

私は、正方形のレイアウトのために自分のクラスを作成していない:カスタムSquare LinearLayout。どうやって?

public class SquareLayout extends LinearLayout{ 

    public SquareLayout(Context context) { 
     super(context); 
    } 

    public SquareLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public SquareLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 


    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     int size = width > height ? height : width; 
     setMeasuredDimension(size, size); 
    } 

その後、私のxmlに:私のJavaコードで書かれた多くの

... 
     <com.myApp.SquareLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center_horizontal" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/cellImageView" 
       android:adjustViewBounds="true" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:padding="2dp" 
       android:scaleType="centerCrop" 
       android:src="@drawable/image" /> 
     </com.myApp.SquareLayout> 
... 

何も。 代わりに私のレイアウトと私のイメージが白い矩形だけを参照しています...

私は間違っていますか?

+0

オープニングタグとしてバージョン番号を調整して追加することを忘れないでください - ' ' ??? – Simon

+0

修正済み:-)ありがとうございます。 – Geltrude

+0

これはhttp://www.gadgetsaint.com/tips/dynamic-square-rectular-layouts-android/#.WRRjMvl96Hs – ASP

答えて

19

//あなたはsuper.onMeasure(widthMeasureSpec、widthMeasureSpec)を呼び出すことを忘れています。

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 
    int size = width > height ? height : width; 
    setMeasuredDimension(size, size); 

} 

// xmlファイル

<com.example.testapplication.SquareLayout 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/cellImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="2dp" 
     android:adjustViewBounds="true" 
     android:padding="2dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/ic_launcher" /> 

    </com.example.testapplication.SquareLayout> 
+0

com.example.testapplication.SquareLayoutはSquareLayoutファイル –

+1

の完全修飾パスです警告:これは役に立ちません'RelativeLayout'を基底クラスとして扱います。私はちょうどこれをやろうとしていましたが、基本クラス 'RelativeLayout'と、子が 'centerInParent'を持っていて、子どもを適切に再レイアウトしていませんでした。代わりに 'LinearLayout'を使うと、完全に動作します。 – karl

9

私はRelativeLayoutに同じ技術を適用する際の問題は、直接setMeasuredDimensionを呼び出していました。私は正しく下端に揃えられませんでした。代わりに、新しい測定仕様でsuper.onMeasure()を呼び出すように変更すると、よりうまく機能しました。

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 
    int size = Math.min(width, height); 
    super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY), 
     MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)); 
    } 
+3

LinearLayout以外の多くの異なるスーパークラスで使用できるので、これは受け入れられた答えよりはるかに柔軟な解決策です。 – Matej

+0

利用可能な回答はすべて似ていますが、子供のサイズの測定に問題があります。これは正解です。ありがとうございます。 –

6

編集:ConstraintLayoutは新しい標準となり、この機能を提供しているように

以下のソリューションは現在、廃止されました。

オリジナル回答:

は、Androidチームは私たちに解決策を与えたが判明し、誰もがそれについて知っています! Percent Support Libraryからこれら2つのクラスをチェックアウト:

ビューの比率を課すしたい場合は、これらのレイアウトの1つに、それを配置する必要があります。そのため、この場合は、サブクラスではなく、標準のLinearLayoutを正しいアスペクト比のレイアウトに配置する必要があります。例あなたはPercentFrameLayout使用したい場合は:あなたが行く

<android.support.percent.PercentFrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_aspectRatio="100%"> 
     <!-- Whatever subviews you want here --> 
    </LinearLayout> 
</android.support.percent.PercentFrameLayout> 

そしてそこに、すべてのあなたの意見は、正方形の線形 レイアウト内に含まれます!

Gradleの依存関係compile 'com.android.support:percent:23.3.0'が必要

+2

アスペクト比は、幅または高さのみが定義されていなければ動作していないように見え、(match_parentの代わりに)固定数として定義されます。 'com.android.support:percent:25.0.1'を使用しています。 – Pang

+0

PercentFrameLayoutとPercentRelativeLayoutの両方が廃止されました。代わりにConstraintLayoutを使用することをお勧めします。 – dzikovskyy

関連する問題