点击自定义条形图(使用MPAndroidChart创建)后,我正在绘制工具提示.视图层次结构如下
<LinearLayout>
<TextView text=Move & Max Pain/>
<RelativeLayout with 2 textviews>
<chart
clipToChildren=false
clipToPadding=false
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
虽然View位于Chart或其中间兄弟中,但外观看起来很好.但是当它与它的兄弟碰撞时,工具提示被截断了

使用HierarchyViewer我可以看到内容存在,但它没有被绘制.
为了获得剪辑,我在draw中使用了这个代码
@Override
public void draw(Canvas canvas, float posx, float posy) {
// take offsets into consideration
posx += getXOffset();
posy += getYOffset();
canvas.save();
// translate to the correct position and draw
canvas.translate(posx, posy);
Rect clipBounds = canvas.getClipBounds();
clipBounds.inset(0, -getHeight());
canvas.clipRect(clipBounds, Region.Op.INTERSECT);
draw(canvas);
canvas.translate(-posx, -posy);
canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)
如果我将Op更改为Region.Op.Replace,则工具提示会正确绘制,但它会替换工具栏内容,而不是在其下滚动.

我正在创建一个聊天应用程序,我正在考虑如何创建实际的聊天视图.
我已经有聊天窗口本身的布局,但我正在考虑如何实现聊天消息.
我正在考虑创建一个TableLayout,每一行将是用户图像和聊天消息(或泡沫或什么不是).
有没有人知道如何设计和创建这些行?
这就是我现在所做的:
<?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:background="@android:color/background_light"
android:orientation="vertical"
android:weightSum="10" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- insert chat message here !-->
</TableRow>
<View
android:layout_height="2dip"
android:background="#000" />
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- next chat message --!>
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="9"
android:orientation="horizontal" >
<EditText
android:id="@+id/chatLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Say:"
android:imeOptions="actionSend"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
android android-custom-view android-layout android-tablelayout
在Activity中,您可以通过以下方式以编程方式创建LinearLayout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
ll.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText("WORLD");
ll.addView(tv2);
setContentView(ll);
}
Run Code Online (Sandbox Code Playgroud)
你如何在自定义View子类中做同样的事情?没有setContentView或onCreate方法......
android android-custom-view android-layout android-linearlayout android-view
假设我有一个可自由聚焦的自定义ViewGroup,并且有一些可以聚焦的子视图(Android机顶盒的自定义垂直菜单应该在远程控制器上作出反应).
每当自定义ViewGroup获得焦点时,我都需要将焦点传递给一些子视图.
我设置descendantFocusability为beforeDescendants并设置OnFocusChangeListener为自定义ViewGroup但OnFocusChangeListener.onFocusChanged()从未调用.它看起来像beforeDescendants我预期的那样不起作用.实际设置beforeDescendants工作与设置相同afterDescendants- 焦点由最近的子视图拍摄,自定义ViewGroup没有机会决定哪个子视图应该关注焦点.
我怎样才能达到理想的行为?在ViewGroups中处理焦点的最佳实践是什么?
Q1:有没有人设法在xml选择器中使用自定义字符串/枚举属性?我通过以下[1]获得了一个布尔属性,但不是字符串属性.
编辑:谢谢你的回答.目前android仅支持布尔选择器.请参阅接受的答案.
我打算实现一个复杂的自定义按钮,其外观取决于两个变量.其他将是布尔属性(true或false)和另一个类别属性(具有许多不同的可能值).我的计划是使用布尔和字符串(或枚举?)属性.我希望我可以使用boolean和string属性在xml选择器中定义UI.
Q2:为什么在[1]中onCreateDrawableState(),布尔属性只有在它们为真时才合并?
注意:这只是一个测试应用程序,用于确定xml选择器中是否可以使用string/enum属性.我知道我可以在没有自定义属性的情况下设置按钮的textcolor.
在我的演示应用程序中,我使用布尔属性将按钮背景设置为dark/bright和string属性以设置文本颜色,{"red","green","blue"}之一.属性定义在/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomButton">
<attr name="make_dark_background" format="boolean" />
<attr name="str_attr" format="string" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
以下是我想要实现的选择器:
@ drawable/custom_button_background(有效)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customstringattribute">
<item app:make_dark_background="true" android:drawable="@color/dark" />
<item android:drawable="@color/bright" />
</selector>
Run Code Online (Sandbox Code Playgroud)
@ color/custom_button_text_color(不起作用)
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customstringattribute">
<item app:str_attr="red" android:color="@color/red" />
<item app:str_attr="green" android:color="@color/green" />
<item app:str_attr="blue" android:color="@color/blue" />
<item android:color="@color/grey" />
</selector>
Run Code Online (Sandbox Code Playgroud)
以下是自定义按钮背景如何连接到布尔选择器,文本颜色连接到字符串选择器.
<com.example.customstringattribute.MyCustomButton
...
android:background="@drawable/custom_button_background"
android:textColor="@color/custom_button_text_color"
...
/>
Run Code Online (Sandbox Code Playgroud)
以下是init()方法中如何加载属性:
private void init(AttributeSet attrs) {
TypedArray …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写由a TextView和EditText_compound_view.xml_ 组成的自定义复合视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/compoundText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label" />
<EditText
android:id="@+id/textEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter text here" >
</EditText>
Run Code Online (Sandbox Code Playgroud)
这是扩展的类LinearLayout:
public class CompoundView extends LinearLayout {
public CompoundView(Context context, AttributeSet attrs) {
super(context, attrs);
readAttributes(context, attrs);
init(context);
}
public CompoundView(Context context) {
super(context);
init(context);
}
private void init(Context c) {
final LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.compound_view, this);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我View在_activity_main.xml_中使用其中的2 个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" …Run Code Online (Sandbox Code Playgroud) 我有一个习惯View,总是Bitmap在一定的轮换.我覆盖了该onDraw方法,旋转Canvas并使用消除锯齿绘制位图Paint.
public RotatedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
someBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder);
}
@Override
protected void onDraw(Canvas canvas) {
// Save and rotate canvas
canvas.save();
canvas.rotate(3F, getWidth() / 2, getHeight());
// Draw the icon
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(someBitmap, 0, 0, p);
canvas.drawRoundRect(new RectF(75, 50, 225, 200), 10, 10, p);
// All done; restore canvas
canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)
但是,我总是在Bitmap上出现锯齿状边缘.请注意,roudned矩形可以很好地消除锯齿边缘.此外,当我应用p.setFilterBitmap(true);此工作(位图表面被过滤/平滑)正确.我错过了什么吗?

这是一个最小的Android项目,其中一个屏幕的隔离示例显示了绘制从资源加载的非抗锯齿Bitmap的视图:https://bitbucket.org/erickok/antialiastest
更新: …
我想实现水平进度条,其步骤如下面的img所示.

我在Android中找不到这样的原生组件.任何人都可以指导我如何做到这一点?
我正在开发一个自定义视图,它实现像圆形菜单这样的Catch应用程序.花了很多时间后,我取得了一些进步,完成了多色的外半圆.现在,阅读Catch应用程序的开发人员为他的查询提供的答案,我遇到了类Path.Google Android Developer页面没有提供足够的材料来理解和熟悉Path.所以,请 ?任何人 ?
提前致谢.
我对此并不太清楚,也不是文档.
当我创建自定义视图时,我会这样重写:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//more code here...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是是否有必要打电话super.onDraw(canvas);.如果没有它,代码似乎工作得很好,但我想确定它可以放弃它.
那有必要吗?