原文提问在:http://ask.csdn.net/questions/22049#answer_5984
用XML布局,能够将TextView控件居中,而代码布局不能将TextView控件居中。
经测试,代码布局中,addView函数对LayoutParams参数添加进的gravity和leftMargin等属性未进行应有的操作,但能够对宽高的设定进行对应的操作。
在NEXUS 4(android 4.3)和我自己的手机(android 4.1.2)上测试,都存在这样的问题。
百度和谷歌都未找到我想要的答案或类似情况。
如果需要LinearLayout里面的控件全部居中的话,将LinearLayout的gravity设置为Gravity.CENTER即可,但我希望一些控件左对齐,一些居中。
希望得到大家的帮助,谢谢!
XML布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
</LinearLayout>
java文件布局代码:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
//setContentView(R.layout.layout)
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
LinearLayout mainLayout = new LinearLayout(this)
mainLayout.setOrientation(LinearLayout.VERTICAL)
setContentView(mainLayout, lp)
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
lp.gravity = Gravity.CENTER
Button button = new Button(this)
button.setText("Button")
mainLayout.addView(button, lp)
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
lp.gravity = Gravity.CENTER
TextView textView = new TextView(this)
textView.setText("TextView")
mainLayout.addView(textView, lp)
}
解决方法:
经测试,是代码的问题,使用下面的代码,可以达到所需效果:
package com.shq.layouttest
import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
//setContentView(R.layout.layout)
ViewGroup.LayoutParams lp_ViewGroup = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
LinearLayout mainLayout = new LinearLayout(this)
mainLayout.setOrientation(LinearLayout.VERTICAL)
setContentView(mainLayout, lp_ViewGroup)
LinearLayout.LayoutParams lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
Button button = new Button(this)
button.setText("Button")
mainLayout.addView(button, lp_LinearLayout)
lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
lp_LinearLayout.gravity = Gravity.CENTER
TextView textView = new TextView(this)
textView.setText("TextView")
mainLayout.addView(textView, lp_LinearLayout)
}
}
不直接使用LayoutParams,而使用对应的LinearLayout.LayoutParams等。