Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android自定义视图中,如何以编程方式设置自定义视图的样式?_Android_Android Layout_Android Custom View - Fatal编程技术网

在android自定义视图中,如何以编程方式设置自定义视图的样式?

在android自定义视图中,如何以编程方式设置自定义视图的样式?,android,android-layout,android-custom-view,Android,Android Layout,Android Custom View,我有一个RelativeLayout的自定义视图“CustomLayout”子类 public class CustomLayout extends RelativeLayout implements View.OnClickListener{ private String titleText; private Context context; private AttributeSet attrs; private ImageView iv1,iv2; private TextView titl

我有一个RelativeLayout的自定义视图“CustomLayout”子类

public class CustomLayout extends RelativeLayout implements View.OnClickListener{
private String titleText;
private Context context;
private AttributeSet attrs;
private ImageView iv1,iv2;
private TextView title,tv2;
private TextView textView;
private Button button;


public CustomLayout(Context context) {
    this(context, null);
}
public CustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.attrs = attrs;
    init();

}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    this.attrs = attrs;
    initAttributes(context,attrs,defStyle);
}
 private void initAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
    // declare styleable here to programatically set custom view style attributes
    final int[] styleable = new int[] {
            android.R.attr.src,
            android.R.attr.textAppearance,
            android.R.attr.text,
            android.R.attr.textSize
    };
    Arrays.sort(styleable);

    TypedArray a = context.obtainStyledAttributes(attrs, styleable, defStyleAttr,0);
    ...
}
我不知道如何通过在最后一个构造函数中传递参数来按语法设置属性,该构造函数从我的活动中获取三个参数。我知道如何从layout.xml文件中执行此操作,如下面的代码所示。请帮助

 <com.example.customview.CustomLayout
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:selectableItemBackground"
    app:titleText="HappyTrips Editors"
    app:descriptText="@string/content"
    app:titleTextColor="#FF0000"
    app:descriptTextColor="#0000FF"
    app:titleTextSize="8sp"
    app:descriptTextSize="6sp"
    app:bgColor="#FFFF00"/>

如何在CustomLayout构造函数中传递包含AttributeSet的参数,从这里在layout.xml中传递
CustomLayout对象=新的CustomLayout(新的ContextThemeWrapper(this,R.style,customStyle))

在layout.xml中,您可以使用用户样式,它将起作用或 CustomLayout对象=新的CustomLayout(新的ContextThemeWrapper(this,R.style,customStyle))

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View result=convertView;
    if (convertView==null){
        result=inflater.inflate(R.layout.layout_list_items,null);
        CustomLayout object = (CustomLayout)result.findViewById(R.id.view1);