Android ConstraintLayout设置match_parent效果

当我们使用ConstraintLayout布局时,会发现无法使用match_parent,如果使用,设置后的控件会铺满整个屏幕。

解决方案:

如果想实现宽度match_parent,就设置宽度为0dp,再设置左约束和右约束;如果想实现高度match_parent,就设置高度为0dp,再设置上约束和下约束。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
package xatu.cn.androidtest_one; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.FileNotFoundException; /* 从手机相册中选择一幅图片并显示在屏幕上 */ public class MainActivity extends AppCompatActivity { private Button btn_choose; private ImageView img_show; private final int REQUEST_PICTURE_CHOOSE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); event(); } private void init() { btn_choose = findViewById(R.id.btn_choose_picture); img_show = findViewById(R.id.imgview_src); } private void event() { btn_choose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_PICK); startActivityForResult(intent, REQUEST_PICTURE_CHOOSE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK){ Uri uri = data.getData(); Log.e("uri", uri.toString()); ContentResolver cr = this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ImageView imageView = (ImageView) findViewById(R.id.imgview_src); /* 将Bitmap设定到ImageView */ imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } } 页面布局代码: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_choose_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择图片" /> <ImageView android:id="@+id/imgview_src" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" xmlns:tools="http://schemas.android.com/tools" tools:context=".Fragment_1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:gravity="center" android:text="wall美图" android:textColor="@color/black" android:textSize="30dp" /> <ViewFlipper android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:inAnimation="@anim/left_in" android:outAnimation="@anim/right_out"> <ImageView android:id="@+id/banner_1" android:layout_width="match_parent" android:layout_height="213dp" android:scaleType="fitXY" app:srcCompat="@mipmap/lbt_1" /> <ImageView android:id="@+id/banner_2" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/lbt_2" /> <ImageView android:id="@+id/banner_3" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/lbt_3" /> <ImageView android:id="@+id/banner_4" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/lbt_4" /> <ImageView android:id="@+id/banner_5" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/lbt_5" /> </ViewFlipper> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/black"/> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>轮播图不会自动轮播
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值