Android開發(fā)(13) 移動View

概述

我們常用的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中。

  1. 我們先獲得該控件的 布局參數(shù) 然后轉(zhuǎn)型為ViewGroup.MarginLayoutParams

  2. 更改margin的數(shù)值沼死,通過更改 該控件的上下左右偏移量(相對于父容器控件的原點)着逐,來更改控件的呈現(xiàn)位置。

  3. 調(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;
        }
    }

代碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市精绎,隨后出現(xiàn)的幾起案子速缨,更是在濱河造成了極大的恐慌,老刑警劉巖代乃,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旬牲,死亡現(xiàn)場離奇詭異,居然都是意外死亡搁吓,警方通過查閱死者的電腦和手機原茅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堕仔,“玉大人擂橘,你說我怎么就攤上這事∧牵” “怎么了通贞?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵朗若,是天一觀的道長。 經(jīng)常有香客問我滑频,道長捡偏,這世上最難降的妖魔是什么唤冈? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任峡迷,我火速辦了婚禮,結(jié)果婚禮上你虹,老公的妹妹穿的比我還像新娘绘搞。我一直安慰自己,他們只是感情好傅物,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布夯辖。 她就那樣靜靜地躺著,像睡著了一般董饰。 火紅的嫁衣襯著肌膚如雪蒿褂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天卒暂,我揣著相機與錄音啄栓,去河邊找鬼。 笑死也祠,一個胖子當著我的面吹牛昙楚,可吹牛的內(nèi)容都是我干的得湘。 我是一名探鬼主播还棱,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼帘睦,長吁一口氣:“原來是場噩夢啊……” “哼翼岁!你這毒婦竟也來了蕉毯?” 一聲冷哼從身側(cè)響起羔沙,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤艾船,失蹤者是張志新(化名)和其女友劉穎琅攘,沒想到半個月后昔字,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體爆袍,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年李滴,在試婚紗的時候發(fā)現(xiàn)自己被綠了螃宙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡所坯,死狀恐怖谆扎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芹助,我是刑警寧澤堂湖,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布闲先,位于F島的核電站,受9級特大地震影響无蜂,放射性物質(zhì)發(fā)生泄漏伺糠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一斥季、第九天 我趴在偏房一處隱蔽的房頂上張望训桶。 院中可真熱鬧,春花似錦酣倾、人聲如沸舵揭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽午绳。三九已至,卻和暖如春映之,著一層夾襖步出監(jiān)牢的瞬間拦焚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工杠输, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留赎败,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓抬伺,卻偏偏與公主長得像螟够,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子峡钓,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容