Android uses the code layout linearlayout to fail to achieve the XML effect (self-answer)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.