如何以编程方式设置 LinearLayout 的重力和布局重力 [英] How to set both gravity and layout gravity of a LinearLayout programatically

查看:30
本文介绍了如何以编程方式设置 LinearLayout 的重力和布局重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我冒着写重复问题的风险,但当我正在研究

这里是xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><线性布局android:id="@+id/llExample"android:layout_width="200dp"android:layout_height="200dp"android:layout_gravity="右|底部"机器人:背景=#e3e2ad"机器人:重力=center_horizo​​ntal"机器人:方向=垂直"><文本视图android:id="@+id/textView1"android:layout_width="100dp"android:layout_height="50dp"机器人:背景=#bcf5b1"android:text="TextView 1"/><文本视图android:id="@+id/textView1"android:layout_width="100dp"android:layout_height="50dp"机器人:背景=#aacaff"android:text="TextView 2"/></LinearLayout></FrameLayout>

以编程方式改变事物

下面的代码展示了如何改变LinearLayoutgravitylayout_gravity.

public class LinearLayoutGravity extends Activity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.linear_layout_gravity);//改变 LinearLayout 的重力(不是 layout_gravity)LinearLayout ll = (LinearLayout) findViewById(R.id.llExample);ll.setGravity(Gravity.CENTER);//改变LinearLayout的layout_gravity(不是重力)FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);params.gravity = Gravity.TOP|Gravity.RIGHT;ll.setLayoutParams(params);}}

结果如下:

另见

I am risking writing a duplicate question, but as I was researching the answer to another SO question, I realized that I couldn't find a simple question and answer that included setting both gravity and layout_gravity of a LinearLayout. Also, I was confused as to the difference between them when talking about a ViewGroup rather than just a view. I am answering my question below.

Here are some of the other questions I viewed:

解决方案

Short answer

Set gravity

linearLayout.setGravity(Gravity.CENTER);

Set layout gravity

// the LinearLayout's parent is a FrameLayout      
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
linearLayout.setLayoutParams(params);

Background

Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.

Setting the gravity of a LinearLayout itself changes the location of the views within it. Setting the layout_gravity of a LinearLayout changes how the LinearLayout is arranged within its parent layout.

This image shows a LinearLayout (brown) within a FrameLayout (white). The LinearLayout's gravity is set to center_horizontal and its layout_gravity is set to right|bottom.

Here is the xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/llExample"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="right|bottom"
        android:background="#e3e2ad"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#bcf5b1"
            android:text="TextView 1" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#aacaff"
            android:text="TextView 2" />

    </LinearLayout>

</FrameLayout>

Changing things programmatically

The following code shows how to change both the gravity and the layout_gravity of the LinearLayout.

public class LinearLayoutGravity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear_layout_gravity);

        // Change the gravity (not layout_gravity) of the LinearLayout
        LinearLayout ll = (LinearLayout) findViewById(R.id.llExample);
        ll.setGravity(Gravity.CENTER);

        // Change the layout_gravity (not gravity) of the LinearLayout      
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
        params.gravity = Gravity.TOP|Gravity.RIGHT;
        ll.setLayoutParams(params);
    }
}

And here is the result:

See also

这篇关于如何以编程方式设置 LinearLayout 的重力和布局重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆