自定义方形LinearLayout.怎么样?

Gel*_*ude 21 xml android android-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);
    }
Run Code Online (Sandbox Code Playgroud)

然后,在我的xml中:

...
        <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>
...
Run Code Online (Sandbox Code Playgroud)

我的java代码中没有更多内容.但相反,如果我的布局和我的图像,我只看到一个白色的矩形......

我错了什么?

Paw*_*dav 20

//你忘记调用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);

}
Run Code Online (Sandbox Code Playgroud)

// 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> 
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 9

setMeasuredDimension在将相同的技术应用于RelativeLayout时,我遇到了直接调用的问题.我无法正确对齐底边.改为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));
  }
Run Code Online (Sandbox Code Playgroud)

  • 这是一个比接受的答案更灵活的解决方案,因为它可以与除LinearLayout之外的许多不同的超类一起使用. (3认同)

Mar*_*ers 6

编辑:

以下解决方案现已弃用,因为ConstraintLayout已成为新标准并提供此功能.

原答案:

原来Android团队给了我们解决方案,但没有人知道它!从百分比支持库中查看这两个类:

如果要强加视图的比例,则必须将其置于其中一个布局中.因此,在这种情况下,您必须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>
Run Code Online (Sandbox Code Playgroud)

你去了,你的所有视图都将包含在方形线性布局中!

不要忘记添加gradle依赖项compile 'com.android.support:percent:23.3.0' 根据需要调整版本号

  • 宽高比似乎不起作用,除非只定义宽度或高度并定义为固定数字(而不是`match_parent`).使用`com.android.support:percent:25.0.1`. (2认同)