Java Android:创建一个循环文本视图?
下面是我当前的简单XML,但是我希望其中的3个文本视图是圆形的,而不是矩形的 我如何才能更改代码以执行此操作Java Android:创建一个循环文本视图?,java,android,textview,xml-layout,Java,Android,Textview,Xml Layout,下面是我当前的简单XML,但是我希望其中的3个文本视图是圆形的,而不是矩形的 我如何才能更改代码以执行此操作 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_p
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSummary1"
android:layout_width="270dp"
android:layout_height="60dp" >
</TextView>
<TextView
android:id="@+id/tvSummary2"
android:layout_width="270dp"
android:layout_height="60dp" >
</TextView>
<TextView
android:id="@+id/tvSummary3"
android:layout_width="270dp"
android:layout_height="60dp" >
</TextView>
</LinearLayout>
注意:我想要一个实际的圆而不是下面所示的曲线边矩形:
编辑:
当前代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSummary1"
android:layout_width="270dp"
android:layout_height="60dp"
android:text=" "
android:gravity="left|center_vertical"
android:background="@drawable/circle"/>
<TextView
android:id="@+id/tvSummary2"
android:layout_width="270dp"
android:layout_height="60dp"
android:background="@drawable/circle" >
</TextView>
<TextView
android:id="@+id/tvSummary3"
android:layout_width="270dp"
android:layout_height="60dp"
android:background="@drawable/circle" >
</TextView>
</LinearLayout>
电流输出:
创建一个texview\u design.xml文件,并用以下代码填充它。将其置于res/drawable模式
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#98AFC7" />
<stroke
android:width="2dp"
android:color="#98AFC7" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
第二种方式(不推荐): 下载此圆圈并将其放置在您的
drawable
文件夹中,然后将其设置为您的TextView的背景。然后将重力
设置为中心
然后它将如下所示:
在您的绘图设备中使用此选项
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#55ff55"/>
<size android:height="60dp"
android:width="60dp"/>
</shape>
将文本视图的背景设置为此在可绘制文件下方尝试。在res/drawable
文件夹中创建名为“circle”的文件,并复制以下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#4a6176" />
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp"
/>
<corners android:radius="10dp" />
</shape>
这是我的实际工作解决方案
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<!-- The fill color -->
<solid android:color="#ffff" />
<!-- Just to add a border -->
<stroke
android:color="#8000"
android:width="2dp"
/>
</shape>
如果你想要一个完美的(未拉伸的)圆圈,请确保你的文本视图宽度和高度匹配(dp相同)
通过缩短文本或扩大圆圈,或缩小文本大小或减少填充(如果有),确保文本适合圆圈。
或上述建议的组合
[编辑]
就我在您的图片中看到的情况而言,您希望在一行中添加太多的文字,而不是纯圆形。
考虑到文本应该有一个<强>平方< /强>方面,所以你可以把它包起来(使用<代码> \n>代码>),或者只把数字放在圆圈中,把上面写的或相应的圆圈放进去。 你可以在<代码>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:color="#22ff55" android:width="3dip"/>
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<size
android:height="60dp"
android:width="60dp" />
</shape>
在文本视图中应用该绘图,如下所示:
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_tv"
android:gravity="center_vertical|center_horizontal"
android:text="ddd"
android:textColor="#000"
android:textSize="20sp" />
输出:
希望这有帮助
编辑:如果文本太长,则首选椭圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:color="#55ff55" android:width="3dip"/>
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<size
android:height="60dp"
android:width="60dp" />
</shape>
输出:
如果你仍然需要一个合适的圆圈,那么我猜你需要在设置文本后动态地设置它的高度,新的高度应该和它的新宽度一样大,以便形成一个合适的圆圈。这里的大部分答案似乎是对形状可绘制的黑客攻击,而android本身通过形状功能支持这一点。这对我来说非常有效。你可以用两种方法来实现
使用固定的高度和宽度,将保持不变
不管你把它放在下面显示的文字
半成品标准成品余量=4dp
典型的解决方案是定义形状并将其用作背景,但随着位数的变化,它不再是一个完美的圆,它看起来像一个带圆边或椭圆形的矩形。所以我开发了这个解决方案,效果很好。希望它能帮助别人
这是自定义文本视图的代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class CircularTextView extends TextView
{
private float strokeWidth;
int strokeColor,solidColor;
public CircularTextView(Context context) {
super(context);
}
public CircularTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircularTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void draw(Canvas canvas) {
Paint circlePaint = new Paint();
circlePaint.setColor(solidColor);
circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
Paint strokePaint = new Paint();
strokePaint.setColor(strokeColor);
strokePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
int h = this.getHeight();
int w = this.getWidth();
int diameter = ((h > w) ? h : w);
int radius = diameter/2;
this.setHeight(diameter);
this.setWidth(diameter);
canvas.drawCircle(diameter / 2 , diameter / 2, radius, strokePaint);
canvas.drawCircle(diameter / 2, diameter / 2, radius-strokeWidth, circlePaint);
super.draw(canvas);
}
public void setStrokeWidth(int dp)
{
float scale = getContext().getResources().getDisplayMetrics().density;
strokeWidth = dp*scale;
}
public void setStrokeColor(String color)
{
strokeColor = Color.parseColor(color);
}
public void setSolidColor(String color)
{
solidColor = Color.parseColor(color);
}
}
然后在XML中,添加一些填充,并确保其重心位于中心
<com.app.tot.customtextview.CircularTextView
android:id="@+id/circularTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11"
android:gravity="center"
android:padding="3dp"/>
它是一个矩形,防止椭圆形背景变成圆形。
将视图设置为正方形将解决所有问题
我发现这个解决方案是干净的,适用于不同的文本大小和文本长度
public class EqualWidthHeightTextView extends TextView {
public EqualWidthHeightTextView(Context context) {
super(context);
}
public EqualWidthHeightTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EqualWidthHeightTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int r = Math.max(getMeasuredWidth(),getMeasuredHeight());
setMeasuredDimension(r, r);
}
}
用法
<com.commons.custom.ui.EqualWidthHeightTextView
android:id="@+id/cluster_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="10+"
android:background="@drawable/oval_light_blue_bg"
android:textColor="@color/white" />
椭圆形、浅蓝色、bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"<br>
android:shape="oval">
<solid android:color="@color/light_blue"/>
<stroke android:color="@color/white" android:width="1dp" />
</shape>
我使用:/drawable/circulo.xml
无需将其复杂化。我也在寻找解决此问题的方法,我发现将矩形文本视图的形状转换为圆形是一件简单易行的事情。使用此方法将是完美的:
在drawable文件夹中创建一个名为“circle.XML”(例如)的新XML文件,并用以下代码填充该文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9FE554" />
<size
android:height="60dp"
android:width="60dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle" />
</selector>
使用此文件,您将创建新形式的TextView。在本例中,我创建了一个绿色的圆圈。如果要添加边框,必须将以下代码添加到上一个文件中:
<stroke
android:width="2dp"
android:color="#FFFFFF" />
使用以下代码在drawable文件夹中创建另一个XML文件(“rounded_textview.XML”):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9FE554" />
<size
android:height="60dp"
android:width="60dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle" />
</selector>
此文件将用于更改TextView使用eligamos的方式
最后,在TextView属性中,我们希望更改方式部分,我们前往“后台”,并选择创建的第二个XML文件(“rounded_TextView.XML”)
通过这些步骤,您可以使用一个圆形来代替TextView TextView矩形。只需更改形状,而不是TextView的功能。结果如下:
此外,我必须指出,这些步骤可以应用于在属性中具有“背景”选项的任何其他组件
祝你好运 1.使用下面的代码在res/drawable文件夹中创建一个xml circle\u text\u bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#fff" />
<stroke
android:width="1dp"
android:color="#98AFC7" />
<corners
android:bottomLeftRadius="50dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp" />
<size
android:height="20dp"
android:width="20dp" />
</shape>
2.使用圆形文本作为文本视图的背景。注意:为了得到一个完美的圆圈,您的文本视图高度和宽度应该相同。请简要说明您的问题。您希望如何使用textview
?发布一些屏幕截图,以便人们能够理解您的要求。我只希望布局中的每个文本视图都是圆形的。为什么-2问???,我在寻找相同的东西。为什么您使用矩形?为什么不使用正确的形状(椭圆形)?我只是用按钮和对话框做的,所以是的,我确定。等等。。那一行不是作为代码添加的。。刚刚编辑。我的错。现在运行它,看看它是否符合您的要求。如果有,则选择此答案。:)伟大的现在,您可以根据需要更改颜色和圆度(在设计xml文件中)。:)这不是创建一个圆,而是创建了一个具有弯曲边的矩形
android:background="@drawable/circulo"
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9FE554" />
<size
android:height="60dp"
android:width="60dp" />
</shape>
<stroke
android:width="2dp"
android:color="#FFFFFF" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle" />
</selector>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H"
android:textSize="30sp"
android:background="@drawable/rounded_textview"
android:textColor="@android:color/white"
android:gravity="center"
android:id="@+id/mark" />
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#fff" />
<stroke
android:width="1dp"
android:color="#98AFC7" />
<corners
android:bottomLeftRadius="50dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp" />
<size
android:height="20dp"
android:width="20dp" />
</shape>