android 控制导航栏内容占据父布局的比例且位置水平居中

工作需求中,需要完成以下的标题布局


当然这种效果是网页实现的,现在需要转成用android 原生效果实现。先说下这个标题文字位置问题

思路:

    1.linearlayout+代码实现

    2.使用linearlayout中weightSum 和 weight_layout 属性达到效果。

第一种方法直接嵌套一个linearlayout,这种方式可以动态添加改变标题内容及个数

	<LinearLayout
 	android:id="@+id/layout_conent"
  	android:layout_width="wrap_content"
  	android:layout_height="match_parent"
  	android:layout_centerHorizontal="true"
 	android:layout_centerVertical="true"
 	android:orientation="horizontal" />
它的父布局可以是RelativeLayout,通过代码new textview 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f);
	params.leftMargin = 35;    ---每个textview 之间的距离
	final TextView tabTextView = new TextView(this);
	tabTextView.setTextAppearance(this, R.style.top_view_item_text);
	tabTextView.setGravity(Gravity.CENTER);
	tabTextView.setId(i);
	tabTextView.setTextColor(Color.BLACK);
	tabTextView.setText(tabItems.get(i));
	tabTextView.setSelected(true);
	if (i == 0) {
	    tabTextView.setTextColor(Color.RED);---默认情况改变颜色
	}
选中变色的情况可以在点击事件中处理;
第二种方法使用android:weightSum + android:layout_weight
父布局中使用weightSum 
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:app="http://schemas.android.com/apk/res-auto"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="50dp"
	    android:orientation="horizontal"
	    android:gravity="center_horizontal"
	    android:weightSum="3"
	    android:background="#000000"
	    >
         <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/colorAccent"
            android:text="按钮"
           />
	</LinearLayout>
根据具体的需求实现的效果如下:

当然如果需要实现成第一张图的效果,这里可以使用button 控件,通过设置改变它的背景色可以做到!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值