如何使Android XML海关视图文件中定义的视图可见

时间:2017-02-04 11:23:44

标签: android xml android-custom-view custom-view

我有以下代码,即在其下方的XML文件中显示一个元素,并调用名为custom view的{​​{1}}

card

以下是XML文件,以及布局在Android工作室中的显示方式:

enter image description here

package tk.zillion.app1; import tk.zillion.app1.CustomViews.Card; public class EmptyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_empty); RelativeLayout rl; rl = (RelativeLayout) findViewById(R.id.activity_empty); rl.addView(new Card(this)); } } 自定义视图如下代码和XML文件:

Card

,XML文件是:

package tk.zillion.app1.CustomViews;

import tk.zillion.app1.R;

public class Card extends RelativeLayout {
    public Card(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public Card(Context context, AttributeSet attrs){
        super(context, attrs);
        init(context);
    }
    public Card(Context context) {
        super(context);
        init(context);
    }
    private void init(Context context) {
        WindowManager windowmanager = (WindowManager) this.getContext()
                                        .getSystemService(Context.WINDOW_SERVICE);

        Display display = windowmanager.getDefaultDisplay();

        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                                         LayoutParams.WRAP_CONTENT);;
        RelativeLayout rl = new RelativeLayout(this.getContext());
        rl.setBackground(context.getDrawable(R.drawable.layer_card_background));
        rl.setMinimumWidth(width);
        Button btn = new Button(this.getContext());
     //   btn.setId(R.id.titleId);
        btn.setId(View.generateViewId());
        TextView one = new TextView(this.getContext());
        one.setText("Device width is: "+String.valueOf(width)+", Device height is: "+String.valueOf(height));
        one.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

        btn.setText(R.string.custom);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "Hello from another toast!";
                Toast toast = Toast.makeText(v.getContext(), text, duration);
                toast.show();
            }
        });
        rl.addView(btn);
        lp.addRule(RelativeLayout.BELOW, btn.getId());
        rl.addView(one, lp);
        // or one.setLayoutParams(lp);  rl.addView(one);
        //  lp.removeRule(RelativeLayout.BELOW);
        this.addView(rl);
    }
}

并在Android工作室中出现:

enter image description here

下面是应用程序如何查看执行(右侧)以及它应该如何(左侧)。 XML文件中定义的视图,即<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_view" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <tk.zillion.app1.CustomViews.Card android:id="@+id/custom_card" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> </tk.zillion.app1.CustomViews.Card> </RelativeLayout> TextView,在执行时不会出现,而实用定义的所有其他元素都是可见的。

enter image description here

我尝试使用My First App,但没有与我合作

我的问题是我如何才能看到XML文件中定义的视图?

2 个答案:

答案 0 :(得分:0)

试试这个xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_view"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TextView android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        />

    <tk.zillion.app1.CustomViews.Card
        android:id="@+id/custom_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

答案 1 :(得分:0)

我找到了答案,并将在下面详细说明,将来可能有人会从中获得帮助:

1。没有必要使用<tk.zillion.app1.CustomViews.Card>,因此我将view_customs.xml重组为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_view"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <TextView android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        />
</LinearLayout>

2。它看起来我错误地使用了inflater,所以重新投入它以匹配:

inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)通胀语句,其中itInflates来自指定XML节点的新视图层次结构。

3。我的Cardinit方法的最终结构变为:

private void init(Context context) {
      // 1. Define the main layout of the CARD class
      RelativeLayout cardLayout = new RelativeLayout(this.getContext());

     // 2. (Optional) set the require specs for this Layout
    cardLayout.setBackground(
              context.getDrawable(R.drawable.layer_card_background));
    cardLayout.setMinimumWidth(width);

    // 3. Inflate the custom view
    LayoutInflater inflater = LayoutInflater.from(context);
    View inflatedLayout= inflater.inflate(R.layout.view_customs, null, false);

    // 4. Set ID for the inflated view
    inflatedLayout.setId(View.generateViewId());

    // 5. (Optional) pick and change the elements in the custom view
    TextView otv = (TextView) inflatedLayout.findViewById(R.id.tv);
    otv.setText("Welcome to my first app");


    /*
       You can avoid point 3, 4 and 5, if you do not want to infalte from existing layout, simply use:
     View inflatedLayout = new View(this.getContext());
    */

    // 6. (Optional) create new elements
    Button btn = new Button(this.getContext());
    btn.setId(View.generateViewId());
    btn.setText(R.string.custom);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "Hello from another toast!";
                Toast toast = Toast.makeText(v.getContext(), text, duration);
                toast.show();
        }
    });

    TextView one = new TextView(this.getContext());
    one.setText("Device width is: ");
    one.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

    // 7. Locate the relative locations between the inflated element and the main Layout
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                                 ViewGroup.LayoutParams.WRAP_CONTENT,
                                 ViewGroup.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.BELOW, inflatedLayout.getId());
    cardLayout.setLayoutParams(p);

    // 8. In the same way as of 7, locate the relative locations of the elements inside the main Layout itself.
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, btn.getId());

   // 9. Add the inflated Layout, and all other elements to the main Layout
   cardLayout.addView(inflatedLayout);
   cardLayout.addView(btn);
   cardLayout.addView(one, lp);

   // 10. Add the main Layout to the class
   this.addView(cardLayout);  

    // 11. In the same way, you need to locate the returned CARD
    // in relative location in the view that calling it
    RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
                                 ViewGroup.LayoutParams.WRAP_CONTENT,
                               //  ViewGroup.LayoutParams.WRAP_CONTENT
                                  150);
    p1.addRule(RelativeLayout.BELOW, R.id.checkBox);
    this.setLayoutParams(p1);
}
  1. (可选)对于自定义卡片可绘制背景,我使用了以下代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
     <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />
     <solid
        android:color="#FFFFFFFF"
        />
    
    <corners
        android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
    </shape>