日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android自定义布局的使用!

發布時間:2025/6/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;
}

}


布局文件;代碼;

<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"
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" >

<com.example.customview1406_04myviwegroup.myviewgroup
android:id="@+id/myViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
>





總結

以上是生活随笔為你收集整理的android自定义布局的使用!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。