LayoutPrams

A layout defines the visual structure for a user interface, such as an activity or widget.

A layout is declared in XML, including screen elements that will appear in it. Code can be added to the application to modify the state of screen objects at runtime, including those declared in XML.

LayoutParams

Every single ViewGroup (e.g. LinearLayout, RelativeLayout, CoordinatorLayout, etc.) needs to store information about its children’s properties. About the way its children are being laid out in the ViewGroup. This information is stored in objects of a wrapper class ViewGroup.LayoutParams.

To include parameters specific to a particular layout type, ViewGroups use subclasses of ViewGroup.LayoutParams class.

E.g. for

  • LinearLayout it’s LinearLayout.LayoutParams
  • RelativeLayout it’s RelativeLayout.LayoutParams
  • CoordinatorLayout it’s CoordinatorLayout.LayoutParams

Most of ViewGroups reutilize the ability to set margins for their children, so they do not subclass ViewGroup.LayoutParams directly, but they subclass ViewGroup.MarginLayoutParams instead (which itself is a subclass of ViewGroup.LayoutParams).


LayoutParams in xml

LayoutParams objects are created based on the inflated layout xml file.

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

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="right"
android:gravity="bottom"
android:text="Example text"
android:textColor="@android:color/holo_green_dark"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:scaleType="centerInside"
android:src="@drawable/example"/>

</LinearLayout>

All parameters that begin with layout_ specify how the enclosing layout should work. When the layout is inflated, those parameters are wrapped in a proper LayoutParams object, that later will be used by the Layout to properly position a particular View within the ViewGroup. Other attributes of a View are directly View-related and are processed by the View itself.

For TextView:

  • layout_width, layout_height and layout_gravity will be stored in a LinearLayout.LayoutParams object and used by the LinearLayout
  • gravity, text and textColor will be used by the TextView itself

For ImageView:

  • layout_width, layout_height and layout_weight will be stored in a LinearLayout.LayoutParams object and used by the LinearLayout
  • background, scaleType and src will be used by the ImageView itself

Getting LayoutParams object

getLayoutParams is a View’s method that allows to retrieve a current LayoutParams object.

Because the LayoutParams object is directly related to the enclosing ViewGroup, this method will return a non-null value only when View is attached to the ViewGroup. You need to bare in mind that this object might not be present at all times. Especially you should not depend on having it inside View’s constructor.

public class ExampleView extends View {

public ExampleView(Context context) {
super(context);
setupView(context);
}

public ExampleView(Context context, AttributeSet attrs) {
super(context, attrs);
setupView(context);
}

public ExampleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setupView(context);
}

private void setupView(Context context) {
if (getLayoutParams().height == 50){  // DO NOT DO THIS!
  // This might produce NullPointerException
doSomething();
}
}

//...
}

If you want to depend on having LayoutParams object, you should use onAttachedToWindow method instead.

public class ExampleView extends View {

public ExampleView(Context context) {
super(context);
}

public ExampleView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ExampleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getLayoutParams().height == 50) { // getLayoutParams() will NOT return null here
doSomething();
}
}

//...
}

Casting LayoutParams object

You might need to use features that are specific to a particular ViewGroup (e.g. you might want to programmatically change rules of a RelativeLayout). For that purpose you will need to know how to properly cast the ViewGroup.LayoutParams object.

This might be a bit confusing when getting a LayoutParams object for a child View that actually is another ViewGroup.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/outer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

<FrameLayout
android:id="@+id/inner_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="right"/>

</LinearLayout>

IMPORTANT: The type of LayoutParams object is directly related to the type of the ENCLOSING ViewGroup.

Incorrect casting:

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) innerLayout.getLayoutParams();
  // INCORRECT! This will produce ClassCastException

Correct casting:

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);
LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) innerLayout.getLayoutParams();
 // CORRECT! the enclosing layout is a LinearLayout
【RIS 辅助的 THz 混合场波束斜视下的信道估计与定位】在混合场波束斜视效应下,利用太赫兹超大可重构智能表面感知用户信道与位置(Matlab代码实现)内容概要:本文围绕“IS 辅助的 THz 混合场波束斜视下的信道估计与定位”展开,重点研究在太赫兹(THz)通信中,由于超大可重构智能表面(RIS)引起的混合近场-远场(混合场)波束斜视效应,对用户信道感知与位置估计带来的挑战。文中提出利用RIS调控电磁波传播特性,结合先进的信号处理算法,在波束斜视影响下实现高精度的信道估计与用户定位,并提供了基于Matlab的代码实现,支持科研复现与进一步优化。研究对于提升未来6G超高速无线通信系统的感知与定位能力具有重要意义。; 适合人群:具备通信工程、信号处理或电子信息等相关专业背景,熟悉Matlab编程,从事太赫兹通信、智能反射面(RIS)或无线定位方向研究的研究生、科研人员及工程师。; 使用场景及目标:① 理解并复现混合场波束斜视效应下的信道建模方法;② 掌握基于RIS的太赫兹系统中信道估计与联合定位算法的设计与实现;③ 为后续开展智能超表面辅助的ISAC(通感一体化)研究提供技术参考和代码基础。; 阅读建议:建议读者结合Matlab代码,深入理解文档中提出的系统模型与算法流程,重点关注波束斜视的数学表征、信道估计算法设计及定位性能评估部分,可通过调整参数进行仿真验证,以加深对关键技术难点和解决方案的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值