用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等。