D T
D T

Reputation: 3756

How can set Height of a View auto by Width?

I have FrameLayout: Width =match_parent, I want set Height = 70% * Width

Ex:

 <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>

How can set Height =70% * Width?

Upvotes: 1

Views: 980

Answers (3)

Juan
Juan

Reputation: 5589

You can use a ViewTreeObserver to know when the layout has completed, and then change the layout params of the FrameLayout to adjust the height.

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();
            }
        });

    }

}

Upvotes: 1

Manish Gupta
Manish Gupta

Reputation: 139

A simple approach is to take that Framelayout inside a Linearlayout and play with weight. No need to use Constraint layout.

<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>

Upvotes: 0

Navneet Krishna
Navneet Krishna

Reputation: 5017

Use 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>

Here just use ratio in app:layout_constraintDimensionRatio to specify the percentage

Upvotes: 4

Related Questions