android自訂布局的使用!

來源:互聯網
上載者:User

android自訂布局的使用!

繼承viewGroup;

自訂控制項的左邊距;右邊距;上邊距,下邊距;


java 代碼;

package com.example.customview1406_04myviwegroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* 自訂的一種布局,指定每個子控制項如何顯示
*
* @author gsd1403
*
*/
public class MyViewGroup extends ViewGroup {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

/**
* 測量控制項的大小
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 測量子控制項的大小
// 子控制項的個數
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
// 得到子控制項
View view = this.getChildAt(i);
view.measure(widthMeasureSpec, heightMeasureSpec);
}
}

public void setShowView(int index) {
if (index > this.getChildCount()) {
return;
}
if (index == 0) {
View view = this.getChildAt(0);
view.setVisibility(View.VISIBLE);
this.getChildAt(1).setVisibility(View.GONE);
} else if (index == 1) {
this.getChildAt(1).setVisibility(View.VISIBLE);
this.getChildAt(0).setVisibility(View.GONE);
}
}

/**
* 指定子控制項如何顯示 類似於onDraw
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 得到第一個控制項 text.xml
// View view1=this.getChildAt(0);
// view1.layout(0, 0, view1.getMeasuredWidth(),
// view1.getMeasuredHeight());
//
// View view2=this.getChildAt(1);
// view2.layout(60, 200, view2.getMeasuredWidth(),
// view2.getMeasuredHeight());
int childCount = this.getChildCount();
int left = 0;
for (int i = 0; i < childCount; i++) {
View view = this.getChildAt(i);
if (view.getVisibility() != View.GONE) {
view.layout(left, 0, left + view.getMeasuredWidth(),
view.getMeasuredHeight());
left = left + view.getMeasuredWidth();
}
}

}

}

MainActivity 代碼;


package com.example.customview1406_04myviwegroup;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
MyViewGroup myViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewGroup=(MyViewGroup) findViewById(R.id.myViewGroup);
Button btnAudio=(Button) findViewById(R.id.button_audio);
btnAudio.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myViewGroup.setShowView(1);
}
});

Button btnText=(Button) findViewById(R.id.button_text);
btnText.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myViewGroup.setShowView(0);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


布局檔案;代碼;


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


android:id="@+id/myViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
>






<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGgxPrXju/dhdWRpb7C0xaXWrrrztcTQp7n7yOfPws28o7s8YnI+CjwvaDE+CjxwPjxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/Collfiles/20141025/2014102508570877.png" alt="">


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.