当前位置: 首页 > 工具软件 > Gravity View > 使用案例 >

代码里面修改layout_gravity

章阳波
2023-12-01
public void change(View view) {
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mImageVew.getLayoutParams();
    layoutParams.gravity = Gravity.CENTER_VERTICAL;
    mImageVew.setLayoutParams(layoutParams);
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:onClick="change"
        android:layout_gravity="center_horizontal"
        />

</LinearLayout>

RelativeLayout

 

public void change(View view) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageVew.getLayoutParams();
        layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mImageVew.setLayoutParams(layoutParams);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:onClick="change"
        />

</RelativeLayout>

 

 类似资料: