使用LayoutParams设置布局
ViewGroup.LayoutParams
extends Object| java.lang.Object | |
| ???? | android.view.ViewGroup.LayoutParams |
Class Overview
LayoutParams are used by views to tell their parents how they want to be laid out.
LayoutParams是ViewGroup的一個內部類, 每個不同的ViewGroup都有自己的LayoutParams子類
DEMO:
//創建一個線性布局 private LinearLayout mLayout; mLayout = (LinearLayout) findViewById(R.id.layout); //現在要往mLayout里邊添加一個TextView TextView textView = new TextView(Activity01.this); textView.setText("Text View " ); //這里是設置 這個textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里邊設置是一樣的如 /**<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text View"/>*///第一個參數為寬的設置,第二個參數為高的設置。 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ); //調用addView()方法增加一個TextView到線性布局中 mLayout.addView(textView, p); //比較簡單的一個例子
LayoutParams繼承于Android.View.ViewGroup.LayoutParams.
LayoutParams相當于一個Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設在屏幕上一塊區域是由一個Layout占領的,如果將一個View添加到一個Layout中,最好告訴Layout用戶期望的布局方式,也就是將一個認可的layoutParams傳遞進去。
可以這樣去形容LayoutParams,在象棋的棋盤上,每個棋子都占據一個位置,也就是每個棋子都有一個位置的信息,如這個棋子在4行4列,這里的“4行4列”就是棋子的LayoutParams。
但LayoutParams類也只是簡單的描述了寬高,寬和高都可以設置成三種值:
1,一個確定的值;
2,FILL_PARENT
3,WRAP_CONTENT
2、addRule()
public classRelativeLayout
extends ViewGroup
| java.lang.Object | |||
| ???? | android.view.View | ||
| ? | ???? | android.view.ViewGroup | |
| ? | ? | ???? | android.widget.RelativeLayout |
| java.lang.Object | |||
| ???? | android.view.View | ||
| ? | ???? | android.view.ViewGroup | |
| ? | ? | ???? | android.widget.RelativeLayout |
void android.widget.RelativeLayout.LayoutParams.addRule(int verb,?int anchor)
public void addRule (int verb, int anchor) 設置控件的相對位置
Added in API level 1Adds a layout rule to be interpreted by the RelativeLayout. Use this for verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean value (VISIBLE).
Parameters
| One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT. |
| The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1. |
package sunny.example.layoutparamstaddrule;import android.content.Context; import android.util.AttributeSet; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.TextView;public class TestView extends RelativeLayout{private LayoutParams mLayoutParams_1,mLayoutParams_2;Button mButton;TextView mTextView;public TestView(Context context) {super(context);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public void init(){mLayoutParams_1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);mLayoutParams_1.addRule(RelativeLayout.ALIGN_TOP); addView(mButton,mLayoutParams_1);mLayoutParams_2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);mLayoutParams_2.addRule(RelativeLayout.BELOW, TRUE);mTextView.setText("Hey");addView(mTextView,mLayoutParams_2);} }
Thanks to 泡在網上的日子
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的使用LayoutParams设置布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux775是什么意思(linux
- 下一篇: View的绘制