简体   繁体   English

android旋转TextView继承父级宽度

[英]android rotated TextView inherits the parent's width

I want to rotate a TextView -90 or 270 degrees and have the resulted overall width equal to the TextView height.我想将 TextView 旋转 -90 或 270 度,并使生成的总宽度等于 TextView 高度。 Here is my mockup:这是我的样机:

在此处输入图片说明

I make my TextView 120dp x 20dp .我使我的 TextView 为120dp x 20dp

I insert it in a parent container (RelativeLayout or any).我将它插入到父容器(RelativeLayout 或任何)中。

I rotate it in XML with -90 degrees but then, when I set its parent to a 20dp width the TextView inherits this width too so it clips my text (clipChildren set to false for the parent has no effect).我在 XML 中以 -90 度旋转它,但是当我将它的父级设置为20dp宽度时,TextView 也继承了这个宽度,因此它剪辑了我的文本(对于父级设置为 false 的 clipChildren 无效)。

The TextView should be dynamic, it should be updated every time with the current month, I don't want to use background drawables for this. TextView 应该是动态的,它应该每次都随当前月更新,我不想为此使用背景可绘制对象。

Well, after several hours of searching the web with no results or with people writing classes to do this and after only 30 minutes since posting the question I managed to achieve what I wanted.好吧,经过几个小时的网络搜索但没有结果,或者有人写课程来做到这一点,并且在发布问题后仅 30 分钟,我就设法实现了我想要的。

I put my TextView in a FrameLayout and now it doesn't inherit its parent width.我将 TextView 放在 FrameLayout 中,现在它不继承其父宽度。 Also gave it a square proportion and a layout_gravity, here's the code:还给了它一个方形比例和一个layout_gravity,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="20dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:rotation="-90"
        android:text="january"
        android:textSize="14sp"
        android:gravity="center|right"
        android:layout_gravity="top|center"
        android:paddingRight="8dp"
        android:background="@color/itemMainBgColor"/>

</FrameLayout>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM