我需要在我的片段中绘制三个圆圈,圆圈的大小不同,我引用此link 我获得的结果是这个
这是我的XML代码:UPDATED
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TextView
android:id="@+id/large_volume"
android:layout_width="185dp"
android:layout_height="185dp"
android:background="@drawable/circle"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
<TextView
android:id="@+id/medium_volume"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignTop="@+id/large_volume"
android:layout_toRightOf="@+id/large_volume"
android:background="@drawable/circle"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
<TextView
android:id="@+id/TextView02"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_below="@+id/medium_volume"
android:layout_toRightOf="@+id/large_volume"
android:background="@drawable/circle"
android:gravity="center"
android:layout_marginTop="-3dp"
android:layout_marginLeft="-17dp"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
但我需要的是这个
你可以看到圈子必须紧密相连,但是当我使用XML视图时,我无法实现这一点。我怎样才能做到这一点,我需要在我的圈子中onClickListeners
,这就是为什么我使用观点
这可以通过Canvas绘图来实现,我听说画布不是视图所以这会限制我给点击听众,如果我错了就纠正我
更新:
我通过XML方法满足了我的需求,有没有办法通过画布绘图实现这一点,我应该发布另一个问题吗?
答案 0 :(得分:38)
试试这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/num_txt"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="0dp"
android:background="@drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
</RelativeLayout>
保存在drawable bg_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="#FF0000"/>
</shape>
已编辑的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="@+id/num_txt"
android:layout_width="185dp"
android:layout_height="185dp"
android:layout_alignParentTop="true"
android:layout_marginTop="163dp"
android:background="@drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dp"
android:textSize="10dp" />
<TextView
android:id="@+id/TextView02"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView01"
android:layout_marginRight="90dp"
android:layout_marginTop="122dp"
android:background="@drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
<TextView
android:id="@+id/TextView01"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignTop="@+id/num_txt"
android:layout_toRightOf="@+id/num_txt"
android:background="@drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
ConstraintLayout用于徽章计数。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="center">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/tab_badge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@drawable/icon_badge"
android:paddingBottom="1dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="1dp"
android:text="10"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@android:color/white"
android:textSize="12dp"
app:layout_constraintBottom_toBottomOf="@+id/tab_icon"
tools:layout_constraintLeft_creator="1"
android:layout_marginLeft="14dp"
app:layout_constraintLeft_toLeftOf="@+id/tab_icon" />
</android.support.constraint.ConstraintLayout>
答案 1 :(得分:8)
试试这个
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);
}
}
答案 2 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/TextView02"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_marginBottom="83dp"
android:layout_toEndOf="@+id/linearLayout1"
android:background="@drawable/circle"
android:gravity="center"
android:text="TextView"
android:layout_marginEnd="5dp"
android:orientation="vertical" />
<TextView
android:id="@+id/textView1"
android:layout_width="185dp"
android:layout_height="185dp"
android:layout_marginStart="5dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/circle"
android:gravity="center"
android:text="TextView"
android:orientation="vertical" />
<TextView
android:id="@+id/TextView01"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginTop="113dp"
android:layout_toEndOf="@+id/linearLayout1"
android:background="@drawable/circle"
android:gravity="center"
android:text="TextView"
android:orientation="vertical" />
</RelativeLayout>
您可以直接将backroud或drawable设置为textview。 你可以使用drawablepadding等.....
或者您可以使用: 的机器人:drawableStart = “@绘制/ ic_cab_done_holo_dark”强>
答案 3 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv);
Button btn = (Button) findViewById(R.id.btn);
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.TRANSPARENT);
gd.setStroke(5, Color.BLUE);
gd.setSize(getTextViewHeight(tv),getTextViewHeight(tv));
tv.setBackground(gd);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.TRANSPARENT);
gd.setStroke(5, Color.BLUE);
gd.setSize(tv.getHeight(),tv.getHeight());
Log.d("DB121","Width "+tv.getHeight()+ "Height ="+tv.getHeight());
tv.setBackground(gd);
}
});
}
public static int getTextViewHeight(TextView textView) {
WindowManager wm =
(WindowManager) textView.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int deviceWidth;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
Point size = new Point();
display.getSize(size);
deviceWidth = size.x;
} else {
deviceWidth = display.getWidth();
}
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
答案 4 :(得分:0)
尝试使用此代码创建带有textview的动态圆圈
RelativeLayout main_rel_layout = findViewById(R.id.main_rel_layout);
TextView textView = new TextView(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setText("Hello ");
textView.setTextSize(100);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
final FrameLayout frameLayout = new FrameLayout(this);
FloatingActionButton floatingActionButton = new FloatingActionButton(this);
floatingActionButton.setCustomSize(400);
frameLayout.addView(floatingActionButton);
relativeLayout.addView(frameLayout);
relativeLayout.addView(textView);
main_rel_layout.addView(relativeLayout);