<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#e3e2e7"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_marginTop="15dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="39dp"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_system_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:src="@drawable/set7" />
<TextView
android:id="@+id/tv_system_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_system_left"
android:layout_centerVertical="true"
android:layout_marginLeft="11dp"
android:text="分享"
android:textColor="#000000"/>
<ImageView
android:id="@+id/iv_system_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:src="@drawable/ios_arrow" />
</RelativeLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@drawable/setborder"
android:layout_marginLeft="46dp"
></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="39dp"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_system_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:src="@drawable/set4" />
<TextView
android:id="@+id/tv_system_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_system_left"
android:layout_centerVertical="true"
android:layout_marginLeft="11dp"
android:text="控制中心"
android:textColor="#000000"/>
<ImageView
android:id="@+id/iv_system_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:src="@drawable/ios_arrow" />
</RelativeLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@drawable/setborder"
android:layout_marginLeft="46dp"
></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="39dp"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_system_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:src="@drawable/set5" />
<TextView
android:id="@+id/tv_system_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_system_left"
android:layout_centerVertical="true"
android:layout_marginLeft="11dp"
android:text="通用"
android:textColor="#000000"/>
<ImageView
android:id="@+id/iv_system_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:src="@drawable/ios_arrow" />
</RelativeLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@drawable/setborder"
android:layout_marginLeft="46dp"
></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="39dp"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_system_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:src="@drawable/set2" />
<TextView
android:id="@+id/tv_system_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_system_left"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:text="最爱"
android:textColor="#000000"/>
<ImageView
android:id="@+id/iv_system_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="11dp"
android:src="@drawable/ios_arrow" />
</RelativeLayout>
</LinearLayout>
LinearLayout实现点击效果,使用seselector
drawable新建myborder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/darkgray" />
<item android:drawable="@drawable/white"></item>
</selector>
styles
<item type="drawable" name="darkgray">#cccccc</item>
<item type="drawable" name="white">#ffffff</item>
android:background="@drawable/myborder"
注意要配置android:clickable="true"
后台生成
public class MySetingFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.main_tab_setting_list_item, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
LinearLayout ly = (LinearLayout)getActivity().findViewById(R.id.my_setting);
ly.addView(CreateSetting(this.getResources().getDrawable(R.drawable.set7),"通用"));
ly.addView(CreateSetting(this.getResources().getDrawable(R.drawable.set6),"视频"));
super.onActivityCreated(savedInstanceState);
}
public int Dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
public LinearLayout CreateSetting(Drawable img,String _text)
{
RelativeLayout rl = new RelativeLayout(getActivity().getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,Dp2Px(getActivity(),39));
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.setLayoutParams(lp);
RelativeLayout.LayoutParams lpiv = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lpiv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
lpiv.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
lpiv.setMargins(Dp2Px(getActivity(),12), 0, 0, 0);
//lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());
ImageView iv = new ImageView(getActivity().getApplicationContext());
iv.setLayoutParams(lpiv);
iv.setImageDrawable(img);
iv.setId(99);//动态相对布局必须设置id,getid才能取到
RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_tv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
lp_tv.setMargins(Dp2Px(getActivity(),11), 0, 0, 0);
lp_tv.addRule(RelativeLayout.RIGHT_OF,iv.getId());
TextView tv = new TextView(getActivity().getApplicationContext());
tv.setLayoutParams(lp_tv);
tv.setText(_text);
tv.setTextColor(Color.parseColor("#000000"));
RelativeLayout.LayoutParams lp_img = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_img.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
lp_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
lp_img.setMargins(0, 0, Dp2Px(getActivity(),12), 0);
//lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());
ImageView ivarrow = new ImageView(getActivity().getApplicationContext());
ivarrow.setLayoutParams(lp_img);
ivarrow.setImageDrawable(this.getResources().getDrawable(R.drawable.ios_arrow));
rl.addView(iv);
rl.addView(tv);
rl.addView(ivarrow);
LinearLayout ll_line = new LinearLayout(getActivity().getApplicationContext());
LinearLayout.LayoutParams lp_line = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,1);
lp_line.setMargins(Dp2Px(getActivity(),46), 0, 0, 0);
ll_line.setLayoutParams(lp_line);
ll_line.setBackgroundColor(Color.parseColor("#cccccc"));
LinearLayout ll_f = new LinearLayout(getActivity().getApplicationContext());
LinearLayout.LayoutParams lp_f = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
ll_f.setLayoutParams(lp_f);
ll_f.setOrientation(LinearLayout.VERTICAL);
ll_f.setClickable(true);
Drawable ll_fd = this.getResources().getDrawable(R.drawable.myborder);
try{
ll_f.setBackgroundDrawable(ll_fd);//还必须要使用这个方法,虽然提示过期
}
catch(Exception e){
ll_f.setBackgroundColor(Color.parseColor("#ffffff"));
}
ll_f.addView(rl);
ll_f.addView(ll_line);
return ll_f;
}
}