【问题标题】:Draw a circle with width match parent绘制一个宽度匹配父级的圆
【发布时间】:2018-12-23 02:55:30
【问题描述】:

我必须向 TextView 添加背景形状,但我不知道这个的尺寸,因为我需要设置宽度以匹配父级。我无法设置尺寸 dp,因为它会导致我在调整大小、键盘显示和其他方面出现更多问题。 我会在创建时获得有效的宽度,并在高度上设置相同的宽度,但是当我写的时候:

TextView tCircle = findViewById(R.id.tCircle);
tCircle.getLayoutParams().width

它给我-1。 这个圆圈怎么画?? button_shape 是:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <corners android:radius="10dp"/>

    <stroke android:color="@color/colorPrimaryDark"
        android:width="2dp"/>
    <solid android:color="@color/colorAccent"/>

</shape>

布局片段

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="6.5"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tCircle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:autoSizeTextType="uniform"
                    android:background="@drawable/button_shape"
                    android:text="TextView"
                    android:textAlignment="center" />
            </LinearLayout>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

编辑: 根据 Mohamed Mohaideen AH 的建议,我在 onCreate 中写道:

tCircle.post(new Runnable() {
      @Override
      public void run() {

          int width = tCircle.getWidth();
          int heigth = tCircle.getHeight();
          Log.d(TAG_LOG, "Dim cerchio: W" + String.valueOf(width)+" H:"+String.valueOf(heigth));

          if(width>=heigth){
              Log.d(TAG_LOG, "A");
              tCircle.setWidth(heigth);
              tCircle.setHeight(heigth);
          }
          else{
              Log.d(TAG_LOG, "B");
              tCircle.setWidth(width);
              tCircle.setHeight(width);
          }

      }
});

TextView 是:

<TextView
android:id="@+id/tCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeTextType="uniform"
android:background="@drawable/button_shape"
android:text="TextView"
android:textAlignment="center" />

但是集合不会改变维度。为什么?

【问题讨论】:

    标签: android android-layout textview android-shape


    【解决方案1】:

    您可以根据屏幕的宽度动态设置高度。使用此方法获取屏幕宽度:

    public static int getScreenWidth(Context mContext) {
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }
    

    然后你可以使用布局参数来设置高度与宽度相同。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,但我的文本视图位于另一个 ecc 中的布局内。所以我需要父亲的尺寸。
    【解决方案2】:

    你得到 -1 的问题是你的视图没有初始化。当OnCreatemethod 加载视图之前,您无法获取布局参数值。所以尝试下面的代码来获得视图的宽度。

    textview.post(new Runnable() {
                @Override
                public void run() {
    
                    int width = textview.getWidth();
    }
    }
    

    已编辑
    为您的文本视图创建一个圆形背景。

    tv.post(new Runnable() {
            @Override
            public void run() {
                int value  = tv.getWidth();              
                tv.setWidth(value);
                tv.setHeight(value);         
          }
    });
    

    希望对你有帮助!

    【讨论】:

    • 好的!它返回我的像素,但为什么什么时候做: textview.setWidth(width), textview.setHeigth(width) 我什么都没改变?
    • @Pab 是 getWidth()、getHeight() 方法仅根据设备密度返回像素值。如果需要,可以从 PX 转换为 dp。
    • Convert pixel value to DP Value 这将帮助您将像素值转换为 dp。
    • 我不是这个意思。我的意思是:现在如何设置宽度和高度?像素或 dp 并不重要。
    • 查看我的编辑部分。您需要的只是将值设置为您的视图。然后你得到了你想要的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2020-11-15
    相关资源
    最近更新 更多