我正在制作一个自定义按钮视图,它具有渐变边框、圆角和透明背景。我将按钮的背景设置为由以下代码生成的可绘制对象:
protected Drawable getBackgroundDrawable() {
GradientDrawable backgroundDrawable = new GradientDrawable(
mOrientation,
mGradientColors);
backgroundDrawable.setCornerRadius(getHeight() / 2);
backgroundDrawable.setShape(GradientDrawable.RECTANGLE);
if (!mFilled) {
Bitmap background = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas backgroundCanvas = new Canvas(background);
backgroundCanvas.drawARGB(0, 0, 0, 0);
backgroundDrawable.setBounds(0, 0, getWidth(), getHeight());
backgroundDrawable.draw(backgroundCanvas);
Paint rectPaint = new Paint();
rectPaint.setAntiAlias(true);
rectPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
backgroundCanvas.drawRoundRect(new RectF(mStroke, mStroke,
getWidth() - mStroke,
getHeight() - mStroke),
getHeight() / 2,
getHeight() / 2,
rectPaint);
return new BitmapDrawable(getResources(), background);
} else {
return backgroundDrawable;
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,当你现在点击按钮时,你没有任何反馈。当我已经使用生成的可绘制对象作为背景时,如何在按钮背面添加波纹效果?我必须用 LayerDrawable 做一些事情吗?
我的按钮看起来像这样:
我真正想要实现的目标
以编程方式创建一个与此 …
我需要垂直移动 ConstraintLayout 中的一些视图,最好具有过渡效果(以避免“跳过”)。
该布局只是一个带有多个小部件(无层次结构)的约束布局,所有小部件都受到父级的约束。
我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
android:orientation="vertical"
android:id="@+id/myLayout"
tools:context="com.example.quant.icarus.MainActivity">
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="48dp"
android:alpha="0.5"
android:background="@drawable/button_border"
android:textColor="@color/ret_text"
app:layout_constraintVertical_bias="0.97"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text" />
<Button
android:id="@+id/button2"
android:layout_width="125dp"
android:layout_height="48dp"
android:alpha="0.5"
app:layout_constraintVertical_bias="0.97"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text" />
<SeekBar
android:id="@+id/prog_bar"
android:layout_width="210dp"
android:layout_height="40dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:thumb="@drawable/seekbar_thumb"
android:max="100"
android:progress="0"
android:rotation="270" />
<TextView
android:id="@+id/prog_text"
android:layout_width="wrap_content"
android:layout_height="20dp"
app:layout_constraintVertical_bias="0.1"
app:layout_constraintLeft_toLeftOf="@+id/prog_bar"
app:layout_constraintRight_toRightOf="@+id/prog_bar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text"/>
<View
android:id="@+id/viewToMove1" …Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-transitions android-constraintlayout
我的应用程序中有一些屏幕,TalkBack 无法推断出正确的阅读顺序。根据文档,我可以使用android:accessibilityTraversalAfter和朋友来改变阅读顺序。但它对我来说不适用于可聚焦的元素,ViewGroup这些元素应该一起阅读。
整个布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:accessibilityTraversalBefore="@id/before"
android:focusable="true"
tools:context=".MainActivity">
<TextView
android:id="@+id/before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:accessibilityTraversalAfter="@id/before"
android:text="Before"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:accessibilityTraversalBefore="@id/after"
android:text="After"
app:layout_constraintBottom_toTopOf="@+id/before"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
它呈现After在屏幕中间的Before底部。我希望 TalkBack 将整个屏幕视为单个连续元素,因此我设置android:focusable为true. 默认情况下,TalkBack 读取为:“之后,之前”。但我希望它读作“之前,之后”。虽然我添加了android:accessibilityTraversalBeforeand android:accessibilityTraversalAfter,但它仍然显示为“之后,之前”。这是节点树调试的输出:
TreeDebug: (-2147455381)429.FrameLayout:(0, 0 - 1080, 1920):A
TreeDebug: (30189)429.TextView:(42, 101 - 397, 172):TEXT{My Application}:A:supportsTextLocation
TreeDebug: (31150)429.ViewGroup:(0, 210 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Kotlin 中创建一个扩展函数。我确实尝试了几个教程,但不太明白如何实现这个教程。
我正在尝试创建一个setWidth()这样的函数
//Find my_view in the fragment
val myView = v.findViewById<RelativeLayout>(R.id.my_view)
//Then use the extension function
myView.setNewWidth(500)
Run Code Online (Sandbox Code Playgroud)
这就是我定义扩展函数的方式
private fun View?.setNewWidth(i: Int) {
val layoutParams: ViewGroup.LayoutParams = View.layoutParams
layoutParams.width = i
View.layoutParams = layoutParams
}
Run Code Online (Sandbox Code Playgroud)
我不明白我需要在这里做什么。
我想将扩展函数称为myView.ExtensionFunction(),但我不知道该怎么做。教程内容不详。
extension-methods android android-view kotlin kotlin-extension
我有一个活动,其中有NavHostFragment. 这NavHostFragment将承载三个片段,其中两个是FragmentA和FragmentB。在 里面FragmentB,我有一个ViewPager2有两页的页面:PageA和PageB,两者实际上都是由一个片段 构建的FragmentC。在每个PageA和里面PageB,我都有一个RecyclerView。
这是 的布局 XML FragmentB。
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/keyline_4"
android:clipChildren="false"
android:clipToPadding="false"
tools:context=".ui.NavigationActivity">
...
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/frag_course_view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/keyline_4"
android:clipChildren="false"
... />
<com.google.android.material.tabs.TabLayout
android:id="@+id/frag_course_tablayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_4"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
如您所见,我已将片段的根布局设置clipChildren为false. 我也将ViewPager2's设置clipChildren为false。PageA …
android android-layout android-view android-recyclerview android-viewpager2
现在我有一个自定义的撰写输入框。BasicTextField,但无论如何我都可以 \xe2\x80\x99t 将输入字体居中。我只能通过Box的offset属性在x方向上进行偏移。
\n之所以要自定义输入框,是因为我需要一个固定高度的输入框和字体大小。但当高度不够时,文本字段无法输入。
\n请帮助我,我更喜欢使用 TextField 编写固定高度的输入框。但在网站上并没有找到合适的解决方案。
\n这是我的自定义代码:
\n@Composable\nfun InputEditText(\n value: String,\n modifier: Modifier,\n onValueChange: (String) -> Unit,\n contentTextStyle: TextStyle,\n hintTextStyle: TextStyle,\n placeHolderString: String = "",\n enabled: Boolean = true,\n readOnly: Boolean = false,\n singleLine: Boolean = false,\n maxLines: Int = Int.MAX_VALUE,\n offsetDp: Dp = 10.dp,\n keyboardOptions: KeyboardOptions = KeyboardOptions.Default,\n keyboardActions: KeyboardActions = KeyboardActions.Default,\n cursorColor: Color = Color.Black,\n) {\n BasicTextField(\n value = value,\n onValueChange = onValueChange,\n modifier = modifier,\n textStyle = contentTextStyle,\n decorationBox = {innerTextField ->\n …Run Code Online (Sandbox Code Playgroud) 我想改变我的屏幕亮度,除了我要显示listView的一小部分.
这是针对Android的DialogInterface,我想知道如何做到这一点.在DialogInterface中,对话框具有正常亮度,所有其他元素在背景上以较小亮度进行.
谢谢.
我正在尝试在活动中实现类似Tab的UI.这就是我想要它的方式.
有3个按钮,每个按钮与LinearLayout相关联.当用户单击按钮时,当前可见布局淡出,与单击按钮关联的布局淡入视图.
如何才能做到这一点?特别是如何使褪色效果?
我正在尝试使用翻译动画将图像视图从屏幕顶部移动到屏幕中央或屏幕中间,即当活动开始时,图像视图开始从屏幕顶部移动到屏幕中间或屏幕中央,其中图像视图空间的顶部和底部图像视图空间在屏幕上显示的完全相同,就像我们在相对布局中所做的一样。在xml文件的布局中,使用父标签中心为true。通常我们会在facebook和whatsapp应用程序中找到这类动画,它们已将它们用于图像的翻译或移动图像视图动画。到目前为止我做了什么。请帮助我解决这些问题。谢谢。
public class MainActivity extends AppCompatActivity {
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imagview);
RelativeLayout root = (RelativeLayout) findViewById(R.id.rel);
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 50);
anim.setInterpolator((new AccelerateDecelerateInterpolator()));
anim.setAnimationListener(new MyAnimationListener());
anim.setDuration(500);
anim.setFillAfter(true);
imageview.startAnimation(anim);
}
});
}
Run Code Online (Sandbox Code Playgroud) 我已经loop在我的代码中添加Views了Layout,下面的代码是一个Loop which is present in another loop,我有这么多的循环,Activity takes so much time to start如何处理这个?
foreach (InfoQuery item in InfoList)
{
if (item == "kitten")
{
if (!done)
{
TextView view= new TextView(ApplicationContext);
view.LayoutParameters = mainLayout.LayoutParameters;
view.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, 3, ApplicationContext.Resources.DisplayMetrics);
view.SetPadding((int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, ApplicationContext.Resources.DisplayMetrics), 0, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, ApplicationContext.Resources.DisplayMetrics), 0);
view.Text = item.position;
layout2.AddView(view,0);
done = true;
}
TextView view2= new TextView(ApplicationContext);
view2.LayoutParameters = mainLayout.LayoutParameters;
view2.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, 3, ApplicationContext.Resources.DisplayMetrics); ;
view2.SetPadding((int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 30, …Run Code Online (Sandbox Code Playgroud) android xamarin.android android-inflate android-view xamarin