很多个ImageView在RelativeLayout中放置一个横排,怎样使整体居中

 我来答
浅笑Ze安然
2015-07-24 · TA获得超过2036个赞
知道小有建树答主
回答量:1043
采纳率:0%
帮助的人:961万
展开全部
package com.example.s_1;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView mTextView;
private ImageView mImageView;
private RelativeLayout mRelativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTextView = (TextView) this.findViewById(R.id.tv);
mRelativeLayout = (RelativeLayout) this.findViewById(R.id.relative_layout);

RelativeLayout.LayoutParams params = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

int tHeight = mTextView.getHeight();
int tWidth = mTextView.getWidth();
System.out.println("width="+outMetrics.widthPixels+"=height="+outMetrics.heightPixels);

Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
int iWidth = drawable.getMinimumWidth();
int iHeight = drawable.getMinimumHeight();

int left = outMetrics.widthPixels/2-iWidth/2;
int top = (outMetrics.heightPixels-tHeight-iHeight)/2;

params.setMargins(left, top, 0, 0);
mImageView = new ImageView(MainActivity.this);
mImageView.setBackgroundResource(R.drawable.ic_launcher);
mRelativeLayout.addView(mImageView, params);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />

</RelativeLayout
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式