android使用代码布局LInearLayout达不到xml的效果(自问自答)

原文提问在: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等。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值