Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 居中文本视图不显示在RelativeLayout中_Android_Textview_Android Relativelayout - Fatal编程技术网

Android 居中文本视图不显示在RelativeLayout中

Android 居中文本视图不显示在RelativeLayout中,android,textview,android-relativelayout,Android,Textview,Android Relativelayout,我有一个文本视图,我需要在正方形内水平和垂直居中。为了做到这一点,我把它放在了一个相对论中: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_par

我有一个文本视图,我需要在正方形内水平和垂直居中。为了做到这一点,我把它放在了一个相对论中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#fff"
    >

    <TextView
        android:id="@+id/centered_text_view_text"
        android:layout_centerInParent="true"
        android:text="test"
        android:background="#00ff00"
        android:textColor="#000"
        android:textSize="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
我的问题是TextView不可见。我可以看到RelativeLayout的白色背景,但是TextView的文本和背景都没有


我也尝试过摆脱RelativeLayout并在TextView上调用layout方法。但是,我无法使其正常工作,因为在我设置TextView上的重力后,文本仅水平对齐,而不是垂直对齐。

首先-自定义视图应扩展某种类型的
视图。class
。要扩展的类应该是
xml
中的任何根视图


第二个-您保留了对
容器布局
(根视图)的引用,这是多余的。这个类是根视图

容器布局
替换为
或完全删除:

textView = (TextView) findViewById(R.id.centered_text_view_text);

第三个-我不能说你怎么通货膨胀是错误的,但我从来没有这样做过

不要在
容器布局
上调用
布局
,而是尝试使用
充气()
方法充气。(您可以在扩展
视图的子类时访问该视图)

请注意,我是如何将
this
设置为根目录,并使用view类中的充气器的


您的班级可能看起来像这样:

public static class TextInLinear extends RelativeLayout { 

     public TextView textView; 

     //region default constructers 

     public void TextInLinear(Context context){
        this(context, null);
     }

     public void TextInLinear(Context context, AttributeSet attrs){
        this(context, attrs, 0);
     }

     public void TextInLinear(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
         if(!isInEditMode()) init();
     }

     //endregion

     private void init(){
         inflate(getContext(), R.layout.view_user_tag, this);
         textView = (TextView) this.findViewById(R.id.centered_text_view_text);
     }


     /* more methods. i.e. setText(), getText() or whatever */
}

你是在什么情况下这样做的?这是视图、活动、片段还是其他内容?这是在自定义视图中。我在网上发布中打电话给“布局”。嗨,谢谢你的评论。我正在尝试您的代码,但程序在我膨胀的行上崩溃:
public静态类TextInLinear{public RelativeLayout containerLayout;public TextView TextView;public TextInLinear(Context Context,Calendar2 root){containerLayout=(RelativeLayout)View.inflate(context,R.layout.centered_text_view,root);textView=(textView)root.findViewById(R.id.centered_text_view_text);}
@DasunCathir-否。您仍然没有像应该的那样使用该视图。如果您正在创建自定义视图,该类应该扩展根视图。请参阅我的更新。请注意,它正在扩展
RelativeLayout
,我删除了您的根视图。
inflate(getContext(), R.layout.view_user_tag, this);
public static class TextInLinear extends RelativeLayout { 

     public TextView textView; 

     //region default constructers 

     public void TextInLinear(Context context){
        this(context, null);
     }

     public void TextInLinear(Context context, AttributeSet attrs){
        this(context, attrs, 0);
     }

     public void TextInLinear(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
         if(!isInEditMode()) init();
     }

     //endregion

     private void init(){
         inflate(getContext(), R.layout.view_user_tag, this);
         textView = (TextView) this.findViewById(R.id.centered_text_view_text);
     }


     /* more methods. i.e. setText(), getText() or whatever */
}