日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用LayoutParams设置布局

發布時間:2023/12/2 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用LayoutParams设置布局 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、public static class

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 class

RelativeLayout

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 1

Adds 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
verbanchor
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设置布局的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。