概述
我們常用的linearlayout,等都屬于流布局腰湾,在流布局中如何移動控件呢? 我決定做個嘗試疆股。雖然可以使用絕對布局费坊,但我不傾向使用這個布局。那么看看我的方式吧旬痹。
margin方式
margin屬性附井,指定邊距讨越。我們就用來它來控制控件的位置,改動它的值將會產(chǎn)生移動的效果永毅。
ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) textView1
.getLayoutParams();
paras.setMargins(paras.leftMargin + 15, paras.topMargin + 15,
paras.rightMargin, paras.bottomMargin);
textView1.requestLayout();
如上面的代碼所示把跨,margin的屬性存在于 布局參數(shù)LayoutParams中。
我們先獲得該控件的 布局參數(shù) 然后轉(zhuǎn)型為ViewGroup.MarginLayoutParams
更改margin的數(shù)值沼死,通過更改 該控件的上下左右偏移量(相對于父容器控件的原點)着逐,來更改控件的呈現(xiàn)位置。
調(diào)用requestLayout 請求重新布局意蛀。
通過上面的方式耸别,我們可以產(chǎn)生控件移動的效果。
ScrollBy方式
同時县钥,我們了解下 ScrollBy這個方法秀姐,該方法可以產(chǎn)生控件的滾動效果。而看起來移動了該控件的子內(nèi)容魁蒜。
textView1.scrollBy(15, 15);
該方法需要兩個參數(shù)囊扳,x軸偏移量和y軸偏移量。執(zhí)行代碼后兜看,我們看到產(chǎn)生了 類似 滾動條移動后锥咸,控件 上移 的效果∠敢疲看起來像是重繪了視圖內(nèi)容搏予,而變化了繪制的坐標原點。
類似的還有個scroolTo方法弧轧,該方法需要指定目的偏移量雪侥。
完整的示例代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:background="#426ab3"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="140dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:background="#ffffff"
android:gravity="center"
android:text="控件1"
tools:context=".MainActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改動marinLeft 控件1" />
<Button
android:id="@+id/btnScroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="scrollBy 控件1" />
<Button
android:id="@+id/btnScrollTo1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="crollTo 控件1" />
<Button
android:id="@+id/btnScrollParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scrollBy 控件1 的父控件" />
</LinearLayout>
<TextView
android:id="@+id/txtState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/LinearLayout1"
android:layout_marginLeft="5dp"
android:layout_marginTop="25dp"
android:text="info:" />
</RelativeLayout>
package com.example.zyf.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView1;
TextView txtState;
Button btn1;
Button btnScroll;
Button btnScrollTo1;
Button btnScrollParent;
LinearLayout linearLayout1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
linearLayout1 = (LinearLayout) findViewById(R.id.LinearLayout1);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// textView1.setPadding(textView1.getPaddingLeft()+15,
// textView1.getPaddingTop(), textView1.getPaddingRight(),
// textView1.getPaddingBottom());
ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) textView1
.getLayoutParams();
paras.setMargins(paras.leftMargin + 15, paras.topMargin + 15,
paras.rightMargin, paras.bottomMargin);
textView1.requestLayout();
//textView1.invalidate();
PrintfState();
}
});
btnScroll = (Button) findViewById(R.id.btnScroll);
btnScroll.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
textView1.scrollBy(15, 15);
//textView1.requestLayout(); //會導(dǎo)致布局重置 而導(dǎo)致失效
PrintfState();
}
});
btnScrollTo1 = (Button) findViewById(R.id.btnScrollTo1);
btnScrollTo1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
textView1.scrollTo(15, 15);
PrintfState();
}
});
btnScrollParent = (Button) findViewById(R.id.btnScrollParent);
btnScrollParent.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
linearLayout1.scrollBy(15, 15);
PrintfState();
}
});
txtState = (TextView) findViewById(R.id.txtState);
PrintfState();
}
private String GetTextStateOfView(View view, String title) {
StringBuilder sb = new StringBuilder(title + "的狀態(tài):\n");
sb.append(String.format("ScrollX:%s ,ScrollY:%s", view.getScrollX(),
view.getScrollY()));
ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) view
.getLayoutParams();
sb.append(String.format("margins: %s,%s,%s,%s", paras.leftMargin,
paras.topMargin, paras.rightMargin,
paras.bottomMargin));
return sb.toString();
}
private void PrintfState() {
String s="";
s += GetTextStateOfView(linearLayout1, "控件1的父 ");
s += GetTextStateOfView(textView1, "\n控件1");
Printf(s);
}
private void Printf(String str) {
txtState.setText(str);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}