簡體   English   中英

以編程方式設置自定義xml布局元素

[英]programmatically setting custom xml layout elements

在我的代碼中,我有一個類標題“ CustomView”,該類標題具有兩個TextView字段並擴展了相對布局。 一個實例化CustomView使用名為“ custom_view_layout”的xml布局來誇大其自身,該布局顯示兩個文本字段,在實例化時,這兩個文本視圖也等同於CustomView的文本視圖。

在我的“主要” xml布局文件中,我有一個“ CustomView”的自定義標簽,您可以從xml中看到該標簽具有一些布局參數。 我要以編程方式實例化CustomView,填充其文本視圖,然后將主xml布局文件中的自定義視圖“設置”為該實例化視圖。 我想以一種比將cusomview標簽布局參數傳遞給我的CustomView實例然后將我的自定義視圖標簽替換為“自定義視圖”實例更流暢的方式來執行此操作。

在我的代碼中,我注釋了兩次失敗的嘗試。 任何建議將不勝感激。

自定義視圖類:

package com.customviewsetexample;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomView extends RelativeLayout {

    TextView textView1;
    TextView textView2;

    public CustomView(Context context) {
        super(context);
        initView(context);
        this.textView1 = (TextView)this.findViewById(R.id.textView1);
        this.textView2 = (TextView)this.findViewById(R.id.textView2);

    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        this.textView1 = (TextView)this.findViewById(R.id.textView1);
        this.textView2 = (TextView)this.findViewById(R.id.textView2);
    }

    private void initView(Context context){
        this.inflate(context, R.layout.custom_view_layout, this);
    }

    public void populateCustomView(String string_1, String string_2){
        System.out.println("Started");
        this.textView1.setText(string_1);
        System.out.println("mid");
        this.textView2.setText(string_2);
        System.out.println("end");
    }

}

我兩次嘗試的活動課都被注釋掉了:

package com.customviewsetexample;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomViewSetExampleActivity extends Activity {
    /** Called when the activity is first created. */

    String string_1;
    String string_2;
    CustomView customView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String string_1 = "String 1";
        String string_2 = "String 2";
        customView = new CustomView(this);
        customView.populateCustomView(string_1, string_2);
        CustomView mainCustomView = (CustomView)findViewById(R.id.mainCustomView);

        /*
         //This Does nothing
        mainCustomView = customView;
         */

        /*
       //Throws Error see below
       mainCustomView.updateViewLayout(customView, mainCustomView.getLayoutParams());
        */
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.customviewsetexample.CustomView     xmlns:android="http://schemas.android.com/apk/res/android"       
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainCustomView"
    >

custom_view_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:text="TextView" android:layout_height="wrap_content"    android:id="@+id/textView1" android:layout_width="wrap_content"    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></TextView>
    <TextView android:text="TextView" android:layout_height="wrap_content"     android:id="@+id/textView2" android:layout_width="wrap_content"    android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView1"></TextView>

來自更新視圖嘗試的錯誤消息:

09-21 01:02:40.121: ERROR/AndroidRuntime(1592): Uncaught handler: thread main exiting due to uncaught exception
09-21 01:02:40.130: ERROR/AndroidRuntime(1592): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customviewsetexample/com.customviewsetexample.CustomViewSetExampleActivity}: java.lang.IllegalArgumentException: Invalid LayoutParams supplied to com.customviewsetexample.CustomView@43762b20
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.os.Looper.loop(Looper.java:123)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at java.lang.reflect.Method.invoke(Method.java:521)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at dalvik.system.NativeStart.main(Native Method)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592): Caused by: java.lang.IllegalArgumentException: Invalid LayoutParams supplied to com.customviewsetexample.CustomView@43762b20
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1759)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.customviewsetexample.CustomViewSetExampleActivity.onCreate(CustomViewSetExampleActivity.java:34)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     ... 11 more

在您試圖對視圖進行充氣的CustomView類中,首先需要注冊Layout充氣服務。

將正確的上下文傳遞給類的構造函數,然后使用傳遞的上下文進行注冊以進行充氣,然后進行充氣。

嘗試類似:

      private void initView(Context context){
        LayoutInflater infl = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View temp = infl.inflate(R.layout.test, null); //contains your entire xml ofcourse
        }

當您嘗試將自定義視圖設置為主xml時,您可以快速完成此操作,例如將主xml膨脹,然后使用.addView(customview) ,如果不反對,可以在執行setContentView()之前先執行此操作您的要求或流程。

這里有很多事情需要重做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM