以编程方式将ImageView放置在屏幕中央

时间:2015-08-20 10:20:39

标签: android

我想以编程方式将ImageView放在屏幕中央。 在xml中,ImageView位于左上角。我尝试用xml来做,但由于某些原因,这是不可能的。 ImageView宽度为200dp,高度为220dp。

这就是我现在所拥有的:

        DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
        float dpHeight = dm.heightPixels / dm.density;
        float dpWidth = dm.widthPixels / dm.density;

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        lp.leftMargin = lp.leftMargin + (int) ((dpWidth / 2) - 100);
        lp.topMargin = lp.topMargin + (int) ((dpHeight / 2) - 110);

7 个答案:

答案 0 :(得分:2)

要以编程方式执行此操作,您可以这样做。

// get the center point of the screen
Point point = new Point();
WindowManager manager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getSize(point);

int centerX = point.x / 2;
int centerY = point.y / 2;

// set the imageview minus half the width and height so its centered
imageview.setTranslationX(centerX - (imageview.getWidth() / 2));
imageview.setTranslationY(centerY - (imageview.getHeight() / 2));

答案 1 :(得分:2)

最后我通过编程方式获得结果,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RL"
>


<ImageView
    android:id="@+id/IV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/faleavatar"

     />

现在转到你的Java页面

 ImageView iv=(ImageView)findViewById(R.id.IV);

    DisplayMetrics metrics = getResources().getDisplayMetrics();

    int DeviceTotalWidth = metrics.widthPixels;
    int DeviceTotalHeight = metrics.heightPixels;

    RelativeLayout RelativeLayoutImageCenter=(RelativeLayout)findViewById(R.id.RL);
    RelativeLayoutImageCenter.setPadding(DeviceTotalWidth/4,DeviceTotalHeight/4,0,0);

答案 2 :(得分:1)

尝试使用XML:

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

   <ImageView 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:src="@drawable/ic_launcher"/>

</LinearLayout>

输出:

Result

答案 3 :(得分:0)

您在哪里将imageview嵌入xml?您的父布局需要宽度和高度宽度match_parent。

如果是这样,您的图片视图需要属性layout_gravity =“center”。这应该适用于xml。

答案 4 :(得分:0)

首先使用RelativeLayout,然后为其添加规则。并且Imageview设置在其中。如下所示。

RelativeLayout.LayoutParams layoutParams = 
(RelativeLayout.LayoutParams)myButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
image.setLayoutParams(layoutParams);

答案 5 :(得分:0)

你可以这样做:

        LinearLayout.LayoutParams myLayout = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        myLayout.gravity = Gravity.CENTER;
        imageView.setLayoutParams(myLayout);

答案 6 :(得分:0)

试试这个:         xml代码是

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativeLayout"
       >
    </RelativeLayout>

在Class文件中试试这个:

   RelativeLayout layout=(RelativeLayout)findViewById(R.id.relativeLayout);
        ImageView imageView=new ImageView(this);
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.width=200;
        params.height=200;
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        imageView.setLayoutParams(params);
        imageView.setImageResource(R.mipmap.ic_launcher);
        layout.addView(imageView);
相关问题