如何通过宽度自动设置视图的高度?

时间:2018-03-29 11:49:23

标签: android android-layout

我有FrameLayout:宽度= match_parent,我想设置Height = 70% * Width

例如:

 <FrameLayout
            android:id="@+id/fiew"
            android:layout_width="match_parent"
            android:layout_height="200dp"    
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp">

        </FrameLayout>

如何设置Height =70% * Width

3 个答案:

答案 0 :(得分:4)

使用ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:background="@android:color/black"
        app:layout_constraintDimensionRatio="H,3:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

此处只使用app:layout_constraintDimensionRatio中的比率来指定百分比

答案 1 :(得分:1)

您可以使用ViewTreeObserver了解布局何时完成,然后更改FrameLayout的布局参数以调整高度。

<强> frame_layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fiew"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/colorPrimary"
        android:layout_marginTop="20dp">

    </FrameLayout>

</LinearLayout>

<强> FrameLayoutActivity

public class FrameLayoutActivity extends AppCompatActivity {
    private FrameLayout fiew;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_layout);

        fiew = (FrameLayout) findViewById(R.id.fiew);


        final View layout = (View)fiew.getParent();
        ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int width  = layout.getMeasuredWidth();
                //int height = layout.getMeasuredHeight();
                ViewGroup.LayoutParams params = fiew.getLayoutParams();
                params.height = (int) (width * 0.7);
                fiew.setLayoutParams(params);
                fiew.invalidate();
            }
        });

    }

}

答案 2 :(得分:0)

一种简单的方法是将Framelayout放在Linearlayout中并使用权重。无需使用Constraint布局。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="10">

      <FrameLayout
        android:id="@+id/fiew"
        android:layout_width="match_parent"
        android:layout_weight="7"
        android:layout_height="0dp">

        </FrameLayout>

    </LinearLayout>