2016-10-20 3 views
0

Ich habe eine Custom View "CustomLayout" Unterklasse von RelativeLayout.In android Benutzerdefinierte Ansicht Wie kann ich meine benutzerdefinierte Ansicht programmgesteuert gestalten?

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); 
    ... 
} 

Ich weiß nicht, wie die Eigenschaften pro-grammatisch zu setzen, indem Argumente im letzten Konstruktor, die von meiner Aktivität drei Argumente übernimmt. Ich weiß es aus layout.xml Datei zu tun, wie in Code gezeigt

<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"/> 

In meinem ListAdapter

@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); 

Wie below.Please helfen Argumente zu übergeben AttributeSet in CustomLayout Konstruktor enthält, von hier

Antwort

1

in layout.xml können Sie Benutzer Stil, es funktioniert oder CustomLayout Objekt = neue CustomLayout (new ContextThemeWrapper (this, R.style, customStyle));

Verwandte Themen