我想在按钮点击上添加一个涟漪动画.我确实喜欢下面但它需要minSdKVersion到21.
ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
按键
<com.devspark.robototextview.widget.RobotoButton
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:text="@string/login_button" />
Run Code Online (Sandbox Code Playgroud)
我想让它向后兼容设计库.
怎么做到这一点?
我有这个items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/colorPreview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="@android:style/TextAppearance.Large"
android:textColor="@android:color/white"
android:id="@+id/colorName" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
当我单独使用它时,selectableItemBackground在我单击视图时动画.但是当我将它用于RecyclerView中的项目时,对点击的影响不再发生.我怎样才能解决这个问题?
PS:这是RecyclerView上的监听器,如果它是相关的:
public ColorListOnItemTouchListener(Context context, OnItemClickListener clickListener) {
mClickListener = clickListener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
if(childView != null && mClickListener != null) {
mClickListener.onItemLongPress(childView, index);
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
if(childView != null …Run Code Online (Sandbox Code Playgroud) 在我的活动中,我正在维护一个SuperActivity,我正在设置主题.
public class SuperActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
}
}
Run Code Online (Sandbox Code Playgroud)
的themes.xml
<!-- ImageBackround -->
<style name="Theme.MyTheme" parent="ThemeLight">
<item name="myBgColor">@color/translucent_black</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在我想在我的一个孩子活动中获取这种颜色.
正如在这个可能的答案中所提到的,我写道:
int[] attrs = new int[] { R.attr.myBgColor /* index 0 */};
TypedArray ta = ChildActivity.this.obtainStyledAttributes(attrs);
int color = ta.getColor(0, android.R.color.background_light);
String c = getString(color);
ta.recycle();
Run Code Online (Sandbox Code Playgroud)
但每次我得到的默认值为android.R.color.background_light&而不是R.attr.myBgColor.
哪里我做错了.我是否传递了错误的背景ChildActivity.this?
我很欣赏Android的FloatingActionButton(fab)功能,并希望在我的项目中的许多不同地方使用它们。
现在,我有类似这样的内容,其中有几个针对它们的xml规范,除了id,icon和onclick外,其他所有规范都是相同的。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabFoo"
android:onClick="onFabFoo"
android:src="@drawable/ic_foo"
app:backgroundTint="?attr/colorButtonNormal"
app2:elevation="2dp"
app:fabSize="mini"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="2dp"
app:rippleColor="?attr/colorSwitchThumbNormal" />
Run Code Online (Sandbox Code Playgroud)
为了避免重复代码...有没有一种方法可以完全以编程方式创建fab,而无需在xml中指定它?
...
试用一些建议...在我将SDK升级到最新版本之前,没有“ setSize”(#25)
FloatingActionButton fab = new FloatingActionButton(this);
fab.setId(View.generateViewId());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("DEBUG", "onFabFoo");
}
});
fab.setImageResource(R.drawable.ic_foo);
fab.setElevation(2);
fab.setSize(android.support.design.widget.FloatingActionButton.SIZE_MINI);
fab.setFocusable(true);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lay.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lay.setMargins(2,2,2,2);
fab.setLayoutParams(lay);
Run Code Online (Sandbox Code Playgroud)
现在还没有想出如何设置颜色
// app:backgroundTint="?attr/colorButtonNormal"
// app:rippleColor="?attr/colorSwitchThumbNormal"
Run Code Online (Sandbox Code Playgroud)
我看到有一些方法可以设置这些颜色(setBackgroundTintList和setRippleColor),但是我看不到如何将其设置为我在原始xml设置中选择的颜色(colorButtonNormal和colorSwitchThumbNormal)
另外,不知道如何将其附加到父项上并显示出来...
好吧,我想我现在意识到,如果你做这一切的程序,那么你就不能使用的功能,如Android Studio中的XML设计视图。因此,工作起来要困难得多。