android自定义布局
㈠ Android自定义layout怎么写
LinearLayout自定义方法有多种:
1、自定义xml布局,然后加载布局,自定义一个View继承LinearLayout
2、在自定义控件中声明它的所有子元素,然后在Layout文件中像使用LinearLayout一样去进行布局。
第二种比较烦 ,它需要在Layout文件中定义好子元素之后,要在代码 onFinishInflate() 进行匹配子元素。
我就说说加载布局文件的方法吧。
首先:定义好layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="主题"
android:textColor="#000000" />
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/home_icon" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/add_icon" />
</LinearLayout>
</LinearLayout>
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 设置图片资源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/**
* 设置显示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<cn.com.demo.view.MyLinearLayout
android:id="@+id/ll_actionbar"
android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span>
android:layout_width="wrap_content"
android:background="@drawable/bg"
/>
</LinearLayout>
接下来自定义一个MyLinearLayout继承LinearLayout,并且加载刚刚写好的layout文件。(比如http://www.tiecou.com)
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 设置图片资源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/**
* 设置显示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
最后,要的时候使用定义好的MyLinearLayout控件。
㈡ android 自定义ListView加自己的布局
Listview使用自定义布局,则需要创建layout,并引用layout。以下为示例代码:
创建layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
2.layout面含有的textview是想要展示内容的地方。那么构建ArrayAdapter时,应该这样写:
ArrayAdapter<String>adapter=newArrayAdapter<String>(this,R.layout.online_user_list_item,R.id.online_user_list_item_textview);
3.ArrayAdapter并且重写getView方法。代码:
public class UserListAdapter extends ArrayAdapter<User> {
private int resourceId;
public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
User user = getItem(position);
LinearLayout userListItem = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resourceId, userListItem, true);
TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);
TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);
TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);
tvUsername.setText(user.getUsername());
tvAskedNum.setText(String.valueOf(user.getAskedNum()));
tvLastMsg.setText(user.getLastMsg());
return userListItem;
}
}
4.将适配器应用到ListView即可,listview.setAdapter(arrayAdapter);即可实现。
㈢ 安卓开发android studio中怎样自定义actionbar的布局
1theme是用于application或activity的。首先打开AndroidManifest文件查看,一般application节点都有默认主题,
2接下来打开上图中theme所在的文件。res-->values-->styles。
3打开后。可以看到,name属性正是步骤一中theme的值。在可以看到parent属性的值,parent是用于继承内置样式的。我们接下来要在该样式的基础上修改。
4修改action bar的背景。可以从图中看到,都是一个引用另一个。图中黄色高亮的部分,是为了兼容性,可以看到其实值是相同的。在这个例子中,因为theme的parent是Theme.AppCompat.Light.DarkActionBar真正起作用的是不带‘android:’前缀的语句,是为了支持低版本的兼容包。而带前缀的语句是API 11以上支持的。
5修改布局背景。这个在layout文件中也可以改,不过在application的theme中修改可以应用于所有activity。
㈣ [Android] 自定义 Dialog 布局设置固定宽高无效
Dialog 的自定义布局的根布局的宽度是写固定的,显示的时候宽度和高度不是对应的固定值。
根布局外面又添加了一层 FrameLayout,设置其宽高均为 wrap_content 来包裹以前的布局。
这个时候猜测是否因为添加自定义视图的时候,布局参数被改写了,然后开始查看源码,最终发现确实是这样的。
在下面的源码分析中,最终发现也是用了 mWindow.setContentView(mAlertDialogLayout) 将 R.layout.alert_dialog.xml 的默认布局添加到 PhoneWindow, 和Activity一样的。
关键的地方看一下 setupCustomContent() 这个方法,在添加自定义视图的时候布局参数设置为 MATCH_PARENT 了,所以我们设置固定大小是没有作用的,要套一层父布局解决这个问题。
㈤ Android 自定义View之Layout过程
系列文章:
在上篇文章: Android 自定义View之Measure过程 ,我们分析了Measure过程,本次将会掀开承上启下的Layout过程神秘面纱,
通过本篇文章,你将了解到:
在上篇文章的比喻里,我们说过:
该ViewGroup 重写了onMeasure(xx)和onLayout(xx)方法:
同时,当layout 执行结束,清除PFLAG_FORCE_LAYOUT标记,该标记会影响Measure过程是否需要执行onMeasure。
该View 重写了onMeasure(xx)和onLayout(xx)方法:
MyViewGroup里添加了MyView、Button两个控件,最终运行的效果如下:
可以看出,MyViewGroup 里子布局的是横向摆放的。我们重点关注Layout过程。实际上,MyViewGroup里我们只重写了onLayout(xx)方法,MyView也是重写了onLayout(xx)方法。
接下来,分析View Layout过程。
与Measure过程类似,连接ViewGroup onLayout(xx)和View onLayout(xx)之间的桥梁是View layout(xx)。
可以看出,最终都调用了setFrame(xx)方法。
对于Measure过程在onMeasure(xx)里记录了尺寸的值,而对于Layout过程则在layout(xx)里记录了坐标值,具体来说是在setFrame(xx)里,该方法两个重点地方:
View.onLayout(xx)是空实现
从layout(xx)和onLayout(xx)声明可知,这两个方法都是可以被重写的,接下来看看ViewGroup是否重写了它们。
ViewGroup.layout(xx)虽然重写了layout(xx),但是仅仅做了简单判断,最后还是调用了View.layout(xx)。
这重写后将onLayout变为抽象方法,也就是说继承自ViewGroup的类必须重写onLayout(xx)方法。
我们以FrameLayout为例,分析其onLayout(xx)做了什么。
FrameLayout.onLayout(xx)为子布局Layout的时候,起始坐标都是以FrameLayout为基准,并没有记录上一个子布局占了哪块位置,因此子布局的摆放位置可能会重叠,这也是FrameLayout布局特性的由来。而我们之前的Demo在水平方向上记录了上一个子布局的摆放位置,下一个摆放时只能在它之后,因此就形成了水平摆放的功能。
由此类推,我们常说的某个子布局在父布局里的哪个位置,决定这个位置的即是ViewGroup.onLayout(xx)。
上边我们分析了View.layout(xx)、View.onLayout(xx)、ViewGroup.layout(xx)、ViewGroup.onLayout(xx),这四者什么关系呢?
View.layout(xx)
View.onLayout(xx)
ViewGroup.layout(xx)
ViewGroup.onLayout(xx)
View/ViewGroup 子类需要重写哪些方法:
用图表示:
通过上述的描述,我们发现Measure过程和Layout过程里定义的方法比较类似:
它俩的套路比较类似:measure(xx)、layout(xx)一般不需要我们重写,measure(xx)里调用onMeasure(xx),layout(xx)为调用者设置坐标值。
若是ViewGroup:onMeasure(xx)里遍历子布局,并测量每个子布局,最后将结果汇总,设置自己测量的尺寸;onLayout(xx)里遍历子布局,并设置每个子布局的坐标。
若是View:onMeasure(xx)则测量自身,并存储测量尺寸;onLayout(xx)不需要做什么。
Measure过程虽然比Layout过程复杂,但仔细分析后就会发现其本质就是为了设置两个成员变量:
而Layout过程虽然比较简单,其本质是为了设置坐标值
将Measure设置的变量和Layout设置的变量联系起来:
此外,Measure过程通过设置PFLAG_LAYOUT_REQUIRED 标记来告诉需要进行onLayout,而Layout过程通过清除 PFLAG_FORCE_LAYOUT来告诉Measure过程不需要执行onMeasure了。
这就是Layout的承上作用
我们知道View的绘制需要依靠Canvas绘制,而Canvas是有作用区域限制的。例如我们使用:
Cavas绘制的起点是哪呢?
对于硬件绘制加速来说:正是通过Layout过程中设置的RenderNode坐标。
而对于软件绘制来说:
关于硬件绘制加速/软件绘制 后续文章会分析。
这就是Layout的启下作用
以上即是Measure、Layout、Draw三者的内在联系。
当然Layout的"承上"还需要考虑margin、gravity等参数的影响。具体用法参见最开始的Demo。
getMeasuredWidth()/getMeasuredHeight 与 getWidth/getHeight区别
我们以获取width为例,分别来看看其方法:
getMeasuredWidth():获取测量的宽,属于"临时值"
getWidth():获取View真实的宽
在Layout过程之前,getWidth() 默认为0
何时可以获取真实的宽、高
下篇将分析Draw()过程,我们将分析"一切都是draw出来的"道理
本篇基于 Android 10.0
㈥ Android 自定义Toast布局
有时候,我们需要自定义Toast布局样式,我们可以通过下面的方法引入一种布局,这样就能展示出来完美的效果;
㈦ Android自定义布局ViewGroup
2.onLayout()方法中,通过view.layout(left,right,top,bottom)。
layout方法会设置该View视图位于父视图的坐标轴
其中left:子布局的左侧到父布局左侧的距离,right:子布局的右侧到父布局左侧的距离,top:子布局的上侧到父布局上侧的距离,bottom:子布局的下侧到父布局上侧的距离。
㈧ 怎么自定义Android标题栏修改TitleBar的布局
Android程序默认的Activity标题栏只能显示一段文字,而且不能改变它的布局、颜色、标题栏的高度等。如果想要在标题栏加上个图标、button、输入框、进度条、修改标题栏颜色等,只能使用自定义的标题栏。自定义标题栏可以通过在onCreate函数中添加以下代码来实现,需要注意的是代码的顺序必须按照下面的样式,否则将无效。
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.mainactivity); //Activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); //标题栏的布局
虽然上面这样可以在标题栏加入一些控件,但是仍然不能改变标题栏的高度、背景色,要想达到这个目的,只能使用theme(主题)。因此往project里先添加一个style。改变背景色修改android:windowTitleBackgroundStyle的值,改变标题栏高度则修改android:windowTitleSize的值。下面是一个示例:
接着再修改AndroidManifest.xml文件,找到要自定义标题栏的Activity,添加上android:theme值,比如:
Java代码
android:theme值就是上面那个style.xml文件里定义的一个style的name值。
按照以上的步骤,修改标题栏布局、高度、背景色的功能就实现了。