先给一个布局代码猜猜他的效果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="240dip"
android:background="@color/colorAccent"
android:onClick="click" />
LinearLayout>
public class FifthActivity extends AppCompatActivity {
private View view1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
view1 = findViewById(R.id.view1);
}
public void click(View view) {
view1.layout(view1.getLeft(), view1.getTop(), view1.getRight(), view1.getBottom() - 100);
}
}
这样的简单代码,应该都能看出执行结果,没错,结果和你想到的一样:
然后换一个方式,用LayoutParams:
package com.xiey94.damp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
public class FifthActivity extends AppCompatActivity {
private View view1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
view1 = findViewById(R.id.view1);
}
public void click(View view) {
ViewGroup.LayoutParams params = view1.getLayoutParams();
params.height = view1.getBottom() - view1.getTop() - 100;
view1.setLayoutParams(params);
}
}
它们的效果是一样的,但是我们刚学习的时候,了解了这两种用法,也只是了解而已,但是,使用的时候,就发现有点不妥了:我不可能永远就只有一个View,我可能要填充满这个界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="240dip"
android:background="@color/colorAccent" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="点击" />
<View
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary" />
LinearLayout>
这时候我用LayoutParams
package com.xiey94.damp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
public class FifthActivity extends AppCompatActivity {
private View view1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
view1 = findViewById(R.id.view1);
}
public void click(View view) {
ViewGroup.LayoutParams params = view1.getLayoutParams();
params.height = view1.getBottom() - view1.getTop() - 100;
view1.setLayoutParams(params);
}
}
嗯,和我们预想的一样,那试试layout
package com.xiey94.damp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class FifthActivity extends AppCompatActivity {
private View view1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
view1 = findViewById(R.id.view1);
}
public void click(View view) {
view1.layout(view1.getLeft(), view1.getTop(), view1.getRight(), view1.getBottom() - 100);
}
}
看看结果:
嗯?怎么不一样了呢?下面的为什么不跟上去呢?
开始我是想不明白的,还去找了找关于layout的讲解,是不是我遗漏了什么,没找到;后来突然才明白,以前我们自定义View的使用,用layout有一个经典的用法:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 循环所有子View
for (int i=0; iint mLeft = Left;
int mTop = Top;
int mRight = Right;
int mBottom = Bottom;
child.layout(mLeft, mTop, mRight, mBottom);
}
}
}
我们是对所有的子View进行layout,然后整个View都跟上改变了;而在我们上面的例子中,我们只对其中的一个View进行了layout改变,而后面的位置信息没变,所以没跟上。而LayoutParams是针对整个的。
如果首尾跟上进度,效果就是一样的了:
package com.xiey94.damp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class FifthActivity extends AppCompatActivity {
private View view1;
private View view2;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
btn = findViewById(R.id.btn);
}
public void click(View view) {
view1.layout(view1.getLeft(), view1.getTop(), view1.getRight(), view1.getBottom() - 100);
btn.layout(btn.getLeft(), btn.getTop() - 100, btn.getRight(), btn.getBottom() - 100);
view2.layout(view2.getLeft(), view2.getTop() - 100, view2.getRight(), view2.getBottom() - 100);
}
}
这样效果就是一样的了: