工作需求中,需要完成以下的标题布局
当然这种效果是网页实现的,现在需要转成用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 textviewLinearLayout.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 控件,通过设置改变它的背景色可以做到!