求这个app界面里面包菜单栏的代码 下面的主页那栏可以用include引用 只求布局

安卓-通过include导入标题栏布局
大家好,今天讲下自定义标题栏,一般实现标题栏大概有这么几种思路:
1)使用系统自带的
2)自定义替换系统自带的
3)自己定义,通过include方式导入
4)自己定义,通过组合控件的方式引用
今天这篇文章要讲的是通过include方式导入标题栏布局,一般项目中每个app的风格是固定的,标题栏的样式最多也就几种,虽然说自定义组合控件的方式可以封装的很灵活,但是一般就用到指定的几种样式,这种情况下完全可以使用include这种方式,大不了多定义几个布局而已,主要看个人喜好了,这个没有对错之分,用户看到的只是效果,并不关心你用什么方式实现的,好了,扯远了啊。
通过include方式导入布局文件实现自定义标题栏的步骤:
1)定义布局资源
2)include布局资源
3)java代码中找到控件处理事件
好,下面分别给出示例代码,有的人就喜欢拿代码直接用,好,满足你的需求:
1)定义布局资源
资源文件名:layout_title_bar.xml
代码:&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="45.0dip"
android:background="@color/colorLan"
android:gravity="top" &
&ImageView
android:id="@+id/title_bar_menu_btn"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_marginTop="3.0dip"
android:gravity="center"
android:src="@drawable/backbtn" /&
android:id="@+id/title_bar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:paddingLeft="75.0dip"
android:paddingRight="75.0dip"
android:singleLine="true"
android:text="我是标题"
android:textColor="#ffffff"
android:textSize="20sp" /&
说明:其中backbtn是一张图片,自己搞一张就行了。这里把图片也提供,省的大家到处找
2)include布局资源
在需要include布局资源的地方的示例代码:&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/windowColor"
android:orientation="vertical"&
&!--标题头--&
&include layout="@layout/layout_title_bar"/&
&!--其他内容部分,此处省去--&
&/LinearLayout&
看到include关键字没有,就这样用就可以了
3)java代码中找到控件处理事件
java代码中可以通过代码的方式设置标题和点击事件
示例代码://处理各种view
private void findView(){
findViewById(R.id.title_bar_menu_btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});//设置返回按钮
((TextView)findViewById(R.id.title_bar_name)).setText("我是标题");//修改标题
set_mListView_adapter();
虽然说技术点不高,但是确实可以解决问题,在项目中也可以直接使用。
好了,今天讲到这里了!!
没有更多推荐了,android中关于如何动态改变include引入的布局的问题
其实在android中,并没有include的相关类。虽然include可以看做是一个View,但在java代码中并不能直接对其的属性layout进行设置。如果要想动态地改变include里面的内容,其实可以通过另外一种方式。
在&LinearLayout&中写入多个include,并设置每个include的id。在代码中将include看做是一个View,并设置其的visibility属性值。注意的是,如果设置某个include不可见,只能使用visibility(View.GONE),不能使用visibility(View.invisibility),他们两者是不同的。View.invisibility只是设置改控件不可见,但在布局文件中的位置还存在,View.GONE不仅控件不可见,而且在布局中的位置会被其后面的控件所替代。
关键代码如下:
&LinearLayout
android:id="@+id/energy_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="7dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:orientation="vertical"
android:layout_weight="7"&
android:id="@+id/include_changeOne"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
layout="@layout/change_one"/&
android:id="@+id/include_histogram"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
layout="@layout/histogram"/&
android:id="@+id/include_stick"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
layout="@layout/stick"/&
&/LinearLayout&
View include_changeOne,include_histogram,include_
include_changeOne = findViewById(R.id.include_changeOne);
include_histogram = findViewById(R.id.include_histogram);
include_stick = findViewById(R.id.include_stick);
if(v==indexOne) {
include_changeOne.setVisibility(View.VISIBLE);
if(v==indexTwo) {
include_changeOne.setVisibility(View.GONE);
include_histogram.setVisibility(View.VISIBLE);
//include_stick.setVisibility(1);
if(v==indexThree){
include_changeOne.setVisibility(View.GONE);
include_histogram.setVisibility(View.GONE);
include_stick.setVisibility(View.VISIBLE);
没有更多推荐了,布局文件-include标签使用
使用include引用布局文件,
没有更多推荐了,https://kuaibao.qq.com/s/XJH200?refer=cp_1026分享分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板扫描二维码扫描关注云+社区最新快讯分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板分享快讯到朋友圈分享快讯到 QQ分享快讯到 QQ 空间分享快讯到微博复制快讯链接到剪贴板扫描二维码扫描关注云+社区Stay Weird, Stay Different
Android:利用Style和include标签来减少布局中的重复代码
XML布局中我们可能会遇到需要重复放置多个相同控件的情况。
好比上图的布局,如果按照常规来做,我们会在布局中放置三个Button,外加两个View作为分界线,其代码如下:
&LinearLayout 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:orientation="vertical"&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="1"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#BFBFBF"/&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="2"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#BFBFBF"/&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="3"
&/LinearLayout&
可以看到,布局中的三个Button属性的几乎相同,唯一不同的就是Button内的内容,而两个作为分界线的View则完全相同,这使得XML布局代码显得过于冗长并且不够简洁美观,可不可以想方法把Android的控件的一些属性像函数一样保存起来,然后在需要的地方调用来大大减少代码的重复性呢?答案是可以的。
Android提供了style(样式)这一机制来修饰控件,它把控件的属性设置并保存好,像函数一样在需要的地方调用即可。
首先,以上述三个Button为例,提取三个Button中相同的属性:
&span style="white-space:pre"& &/span&android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"这些就是三个Button相同的属性,接下来要做的就是把他们添加到style中。
如图在values文件夹下的styles.xml文件就是存放样式的地方。
打开styles.xml,可以看到在&resources&标签中定义着几个预先就有的style
例如左图中的这句,它的意思就是该style的名字为AppBaseTheme,可以理解为函数名,调用的时候需要用到这个名字。而后半句则是说该style的父母,作用就跟面向对象中的继承一样,该style继承了指定的父母的style,只要理解了面向对象中继承的概念,应该很好懂,这里不细说。
回归正题,怎样把三个Button的共同点存放到style中?
很简单,在styles.xml下 新增一对&style&标签,为该style命名,如命名为DemoBtn,然后在&style&标签内 使用&item&标签来存放控件的属性,例如 android:layout_width="match_parent"
,用&item&来表示就是 &item name="android:layout_width"&match_parent&/item&,name是控件的属性名,标签之间则是属性值
按照这样的方法,把所有共同属性都添加到DemoBtn这一style中即可。
&style name="DemoBtn"&
&item name="android:layout_width"&match_parent&/item&
&item name="android:layout_height"&wrap_content&/item&
&item name="android:textSize"&16dp&/item&
&item name="android:gravity"&center&/item&
&item name="android:layout_marginTop"&5dp&/item&
&item name="android:layout_marginBottom"&5dp&/item&
最后回到布局中,把三个Button的共同属性全都删除掉,换为 style ="@style/DemoBtn"
,就能达到原来的效果,1行代码代替6行代码,代码现在更加美观了,当然,我们的工作还没结束,还有可以简化的空间,别忘记了两个View是完全相同的,同样的,我们也可以使用style来简化代码,但是这里因为两个控件是完全相同,就可以用一种更加简洁方便的方法来简化代码,减少重复性 -----使用include标签。
其实include标签没什么好说的,使用方法相当简单,把View单独写在一个xml文件里,然后用include标签引用它即可。
例如我们把之前布局文件中的View分割出来单独写在了test.xml中,那么我们只需要把原布局中的两个View组件全部删除,都换为一行代码 &include layout="@layout./test" /&即可。
下面给出简化后的布局代码:
&LinearLayout 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:orientation="vertical"&
style="@style/DemoBtn"
android:text="1"
&include layout="@layout/divider_view"/&
style="@style/DemoBtn"
android:text="2"
&include layout="@layout/divider_view"/&
style="@style/DemoBtn"
android:text="3"
&/LinearLayout&
divider_view:
&?xml version="1.0" encoding="utf-8"?&
&View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#BFBFBF"&
怎么样,是不是比原来的布局代码简洁直观多了,而且实现出来的效果也一样。当然,这篇博客只是抛个砖,style和include标签的用法还有很多,只有用多了才知道在哪里用,怎么用最好,其他就要靠自己体会了。
没有更多推荐了,

我要回帖

更多关于 tab栏界面 的文章

 

随机推荐