如何设置的LinearLayout的重力和布局重心编程 [英] How to set both gravity and layout gravity of a LinearLayout programatically
问题描述
我冒着写一个重复的问题,但我研究的答案另一个SO质疑 ,我意识到,我无法找到一个简单的问题和答案,其中包括设置重力和一个的LinearLayout
的layout_gravity。另外,我是困惑,当说起的ViewGroup
,而不仅仅是一个观点它们之间的区别。我在下面回答我的问题。
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:
- 如何设置重力布局编程? 李>
- 设置重力的LinearLayout编程
- <一个href="http://stackoverflow.com/questions/9678785/android-setting-layoutparams-programmatically">android - 设置的LayoutParams编程
- 的LayoutParams比重不工作
- layout_gravity线性布局
- <一个href="http://stackoverflow.com/questions/4948914/problem-with-setlayoutparams-method-in-android-ui">Problem与setLayoutParams在Android的UI 方法
- <一个href="http://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements">how设置setLayoutParams线性布局元素
- <一个href="http://stackoverflow.com/questions/5151056/is-it-possible-to-change-the-layout-width-and-height-in-android-at-run-time">Is有可能改变布局宽度和高度的Android在运行时?
- How to set gravity to layout programmatically?
- Set gravity for LinearLayout programmatically
- android - setting LayoutParams programmatically
- LayoutParams gravity not working
- layout_gravity in linear layout
- Problem with setLayoutParams method in android UI
- how to set setLayoutParams for linear layout elements
- Is it possible to change the layout width and height in Android at run time?
推荐答案
previously,我已经'重力'和'layout_gravity'之间的差别为布局中的观点解释
Background
Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.
设置的的LinearLayout
本身的变化范围内它的意见位置重力
。设置的layout_gravity一个的LinearLayout
如何改变的的LinearLayout
被安排其父布局中。
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.
此图显示了一个的LinearLayout
(棕色)在的FrameLayout
(白色)。该的LinearLayout
的重力
设置为 center_horizontal
及其 layout_gravity
设置为右|。底部
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
.
下面是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>
更改的事情编程
下面code说明了如何同时更改的<$ C引力
和 layout_gravity
$ C>的LinearLayout 。
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;
params.gravity = Gravity.RIGHT;
ll.setLayoutParams(params);
}
}
这里是结果:
And here is the result:
这篇关于如何设置的LinearLayout的重力和布局重心编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!