android使用代碼布局LInearLayout達不到xml的效果(自問自答)

來源:互聯網
上載者:User

原文提問在:http://ask.csdn.net/questions/22049#answer_5984

用XML布局,能夠將TextView控制項置中,而代碼布局不能將TextView控制項置中。
經測試,代碼布局中,addView函數對LayoutParams參數添加進的gravity和leftMargin等屬性未進行應有的操作,但能夠對寬高的設定進行對應的操作。
在NEXUS 4(android 4.3)和我自己的手機(android 4.1.2)上測試,都存在這樣的問題。
百度和Google都未找到我想要的答案或類似情況。
如果需要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等。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.