Question in: http://ask.csdn.net/questions/22049#answer_5984
With XML layout, textview controls can be centered, while code layout cannot.
After testing, in the Code layout, the addview function does not perform proper operations on the gravity and leftmargin attributes added to the layoutparams parameter, but can perform corresponding operations on the width and height settings.
This problem exists in Nexus 4 (Android 4.3) and my own mobile phone (Android 4.1.2.
Neither Baidu nor Google found the answer or similar situation I wanted.
If you need to center all the controls in linearlayout, set gravity of linearlayout to gravity. Center, But I want some controls to be left aligned and centered.
Thank you for your help!
XML layout file code:
<?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 file layout code:
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);}
Solution:
After testing, it is a code problem. The following code can achieve the desired effect:
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); }}
Use linearlayout. layoutparams instead of layoutparams.