Hey. I need to create my component that will be inherited from RelativeLayout. To describe the styles of its elements I want to use layout xml. If I create a button programmatically and add it to a custom component, then it works fine: - I can access the button and programmatically change the text, or color - make the handler
public void init(Context context) { this.context = context; inflate(getContext(), R.layout.native_ad, this); this.adWebView = (WebView) findViewById(R.id.nativeAdWebView); this.adTitle = (TextView) findViewById(R.id.nativeAdTitle); //this.adButton = (Button) findViewById(R.id.nativeAdButton); не работает this.adButton = new Button(context); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); this.addView(adButton, params); adWebView.setDrawingCacheEnabled(true); adWebView.getSettings().setJavaScriptEnabled(true); adWebView.buildDrawingCache(); adWebView.setBackgroundColor(Color.BLUE); adWebView.setWebViewClient(new MyWebClient()); } How can I make it see a button from an xml file? With TextView, everything works fine.
public class CustomComponent extends RelativeLayout { WebView adWebView; TextView adTitle; Button adButton; Context context; public CustomComponent (Context context) { super(context); this.context = context; init(context); } public CustomComponent (Context context, AttributeSet attrs) { super(context, attrs); this.context = context; init(context); } public CustomComponent (Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; init(context); } all the elements are written in the hml file itself, but only TextView normally sees and works. A similar problem with WebView, but I think that they have one solution.
xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:id="@+id/nativeAdTitle"/> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/nativeAdWebView" android:layout_below="@+id/nativeAdTitle" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> </WebView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/nativeAdButton" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" /> </RelativeLayout> 