【问题标题】:Placing an ImageView in centre with width and height to be half of parent width?将 ImageView 置于中心,宽度和高度为父宽度的一半?
【发布时间】:2016-01-13 06:14:59
【问题描述】:

我想在 ViewGroup(最好是 RelativeLayout)中放置一个 ImageView,如下所示。视图组被拉伸以占据活动的全部宽度和高度。我想确保无论设备宽度是什么,ImageView 的高度和宽度都应该是它的一半..

╔═══════════════════════╗   
║                       ║ 
║       View Group      ║ 
║                       ║ 
║    ╔═════════════╗    ║   
║    ║             ║    ║   
║    ║ ImageView   ║    ║   
║    ║             ║    ║   
║    ║ width = X/2 ║    ║   
║    ║ height= X/2 ║    ║   
║    ╚═════════════╝    ║   
║                       ║ 
║                       ║ 
║                       ║ 
╚═══════════════════════╝   
<----------------------->
Xpx width of Device/Viewgroup

有人可以帮助我如何实现这一目标吗?

【问题讨论】:

标签: android android-imageview


【解决方案1】:

也许可以试试这样的:

public class CenteredImageView extends ImageView {


public CenteredImageView(Context context) {
    super(context);
}

public CenteredImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CenteredImageView(Context context, AttributeSet attrs, int id) {
    super(context, attrs, id);
}

@Override
public void onMeasure(int measuredWidth, int measuredHeight) {
    super.onMeasure(measuredWidth, measuredHeight);
    ViewGroup parent = (ViewGroup) getParent();
    if(parent != null){
        int width = parent.getMeasuredWidth() / 2;
        setMeasuredDimension(width, width);
    }
}

可能不需要对父级进行 null 检查,但应相应地调整图像大小

【讨论】:

  • 谢谢你的回答很有帮助:)
【解决方案2】:

要让 ImageView 正方形像这样实现一个 SquareImageView

public class SquareImageView  extends ImageView {
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){    
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = getMeasuredHeight();
        setMeasuredDimension(height, height);
}

然后将其添加到带有verticl的LinearLayout以及两个虚拟的不可见视图(之前和之后),全部使用layout_height=0并设置为SquareImageViewlayout_weigth=0.5以及其他两个每个layout_weigth=0.25 /p>

【讨论】:

  • 他说以父母的一半宽度和一半高度居中
  • 从他的描述中我看到它的平方 width = X/2 height= X/2
  • True, "Xpx width of Device/Viewgroup" 没看到那部分
【解决方案3】:

看看这个(我已经加了cmets)

activity.main.xml

<?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="com.example.piotr.myapplication.MainActivity"
    android:id="@+id/content"
    android:gravity="center">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/centered"/>

</RelativeLayout>

MainActivity.java

//Set layout Gravity as centered
        RelativeLayout content = (RelativeLayout) findViewById(R.id.content);
        content.setGravity(Gravity.CENTER);

        //This is your image which would be in the middle
        ImageView centeredImage = (ImageView) findViewById(R.id.centered);

        //get Screen Dimensions
        DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        //NOTE: If you want to square, just use one of these value.

        //set as half of dimens
        centeredImage.getLayoutParams().height = height/2;
        centeredImage.getLayoutParams().width = width/2;

        //Add an color
        centeredImage.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

        //Also possible
        //centeredImage.setBackgroundColor(Color.BLUE);


        //Add image from resources
        //centeredImage.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));

将此代码放入您的MainActivity 类的onCreate 方法中。

最后,这是一个最终效果:

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多