简体   繁体   English

如何使可见的Android XML习俗视图文件中定义的视图

[英]How can I make the Views defined in Android XML customs view file visible

I've the below code, that is having one element as shown in the XML file below it, and a call of the custom view named card 我有以下代码,它具有一个元素,如其下面的XML文件所示,并调用了名为cardcustom view

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

Below is the XML file, and how the layout appear in the Android studio: 以下是XML文件,以及布局在Android Studio中的显示方式:

在此处输入图片说明

The Card custom view is as below code and XML file: 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);
    }
}

and the XML file is: 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>

and appeared in Android studio as: 并在Android Studio中显示为:

在此处输入图片说明

Below is how the app looks at execution (Right side), and how it should be (Left side). 下面是该应用在执行时的外观(右侧)以及应如何(左侧)。 The View defined in the XML file, that is the My First App TextView, is not appearing at the execution, while all other elements defined pragmatically are visible. XML文件中定义的View(即“ My First App TextView)不会在执行时显示,而所有其他实用地定义的元素都是可见的。

在此处输入图片说明

I tried to use inflate but did not work with me 我尝试使用inflate但无法与我一起使用

My Question is how can I the View, the ones defined in the XML file are visible? 我的问题是,如何查看XML文件中定义的视图?

Try this xml 试试这个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>

I found the answer, and will detailed it below, may some one in the future get help from it: 我找到了答案,并将在下面进行详细说明,将来可能有人会从中获得帮助:

1 . 1。 there was no need for using <tk.zillion.app1.CustomViews.Card> so I restructured my view_customs.xml to be: 无需使用<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 . 2。 It looks I was wrongly using the inflater, so re-wote it to match the: 看来我错误地使用了充气机,因此请重新使用它以匹配:

inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) inflation statement, where itInflates a new view hierarchy from the specified XML node. inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)膨胀语句,其中它从指定的XML节点膨胀新的视图层次结构。

3 . 3。 The final structure of my Card class init method, became: 我的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. (Optional) for the custom card drawable background, I used the below code: (可选)对于自定义卡片可绘制背景,我使用了以下代码:

     <?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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用布局XML文件中定义的视图作为模板来以编程方式创建视图 - How can i make use of views defined in layout XML file as template to create views programmatic way 我怎样才能这样一个android xml视图 - How Can I make a android xml view as this 在Android中,如何访问清单文件中未定义的视图? - In Android, how can I access a view that is not defined in the manifest file? 如何转换 XML 以在 Android Java 中以编程方式查看 - How can I convert an XML to make a programmatically view in Android Java 如何制作从 xml 文件中获取视图的视图 - How to make a view that takes views from xml file 如何动态创建海关视图? - How to create customs views dynamically? 如何在 res/layout/main.xml 文件中使用自定义视图 - how can i use self-defined view in the res/layout/main.xml file 如何动态将布局和视图添加到Android XML布局文件? - How can I dynamically add layouts and views to an Android XML layout file? 如何使Listview项目视图在onLongPress上可见 - How to make Listview items views view Visible onLongPress 如何使 wrap_content 文本视图仅将其内容包裹起来,以使其他视图在 LinearLayout android 中仍然可见? - How to make a wrap_content text view wrap its content only enough to still make other views visible in LinearLayout android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM