我正在写一些自定义视图,它们共享一些同名的属性.在他们各自的<declare-styleable>部分,attrs.xml我想对属性使用相同的名称:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView1">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
<declare-styleable name="MyView2">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,说明myattr1并且myattr2已经定义了.我发现我应该省略formatfor myattr1和myattr2in 的属性MyView2,但如果我这样做,我在控制台中获得以下错误:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
Run Code Online (Sandbox Code Playgroud)
有没有办法可以实现这一点,也许某种命名空间(只是猜测)?
android android-custom-view android-view android-custom-attributes
在创建自定义视图时,我注意到许多人似乎这样做:
public MyView(Context context) {
super(context);
// this constructor used when programmatically creating view
doAdditionalConstructorWork();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// this constructor used when creating view through XML
doAdditionalConstructorWork();
}
private void doAdditionalConstructorWork() {
// init variables etc.
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,构造函数MyView(Context context, AttributeSet attrs, int defStyle)怎么样?我不确定它在哪里使用,但我在超级课程中看到它.我需要它,它在哪里使用?
我创建了一个自定义视图(在此处找到),其中包含enum类型的declare-styleable属性.在xml中,我现在可以为自定义属性选择一个枚举条目.现在我想创建一个以编程方式设置此值的方法,但我无法访问枚举.
attr.xml
<declare-styleable name="IconView">
<attr name="icon" format="enum">
<enum name="enum_name_one" value="0"/>
....
<enum name="enum_name_n" value="666"/>
</attr>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
layout.xml
<com.xyz.views.IconView
android:id="@+id/heart_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="enum_name_x"/>
Run Code Online (Sandbox Code Playgroud)
我需要的是:mCustomView.setIcon(R.id.enum_name_x);
但是我找不到枚举,或者我甚至不知道如何获得枚举或枚举的名称.
我想做的是:我正在尝试制作自定义对话框.圆角.
什么是happing:我能够进行自定义对话,但它没有圆角.我尝试添加一个选择器,但我还是不能
Java代码:
private void launchDismissDlg() {
dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dlg_dismiss);
dialog.setCanceledOnTouchOutside(true);
Button btnReopenId = (Button) dialog.findViewById(R.id.btnReopenId);
Button btnCancelId = (Button) dialog.findViewById(R.id.btnCancelId);
btnReopenId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnCancelId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
xml代码:
<?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/white"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我必须编辑一个具有自定义视图的软件,当我尝试编辑布局xml时,Eclipse说我:
在Eclipse中显示时,在自定义视图中使用View.isInEditMode()来跳过代码
但我不知道应用程序中必须使用isIndditMode()的方式和位置
我的xml文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="32dip"
android:scrollbars="none"
android:lines="1"
android:freezesText="true"
android:textColor="@color/result"
/>
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="28dip"
android:scrollbars="none"
android:singleLine="true"
android:autoText="false"
android:imeOptions="flagNoEnterAction|flagNoExtractUi"
/>
<ListView
android:id="@+id/history"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#ff000000"
android:choiceMode="singleChoice"
android:scrollbarStyle="outsideInset"
android:scrollbars="none"
/>
<calculator.GraphView
android:id="@+id/graph"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:visibility="gone"
/>
<include layout="@layout/keyboard" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的GraphView是
public class GraphView extends View implements Grapher,
ZoomButtonsController.OnZoomListener,
TouchHandler.TouchHandlerInterface {
private int width, height;
private Matrix matrix = …Run Code Online (Sandbox Code Playgroud) 有没有方法可以将snackBar的布局更改为自定义View?
现在它变黑了,我们可以改变背景颜色.但我不知道正确的方法来膨胀新的布局并使其成为snackBars背景?
谢谢...
android android-custom-view android-layout snackbar android-snackbar
编辑问题:
移动分辨率:
我想设计不同的屏幕dpi,如下面的分辨率.
小320x480,
480×800,
540x960,
720x1280(三星S3),
1080×1920(S4,Nexus5,纳克斯5X,摩托G4),
2560 X 1440(的Nexus 6,纳克斯6P,三星边缘)
平板分辨率:
480×800(Micromax的),
600x1024(三星tab2),
800x1280(nexus 7),
1200x1920(新nexus 7),
2048x1536(nexus 9)
我想根据设备显示分辨率使用不同的字体大小.
Q1)best解决这个问题的方法是什么problem?
Q2)什么是抛出编码或XML的最佳选择?
Q3)哪个drawable文件夹代表哪个设备分辨率?
Q4)应用程序启动器图标大小为不同的分辨率?
android screen-resolution android-custom-view android-xml android-drawable
View Binding 作为 Android Jetpack 的一部分发布
文档:https : //developer.android.com/topic/libraries/view-binding
我的问题是,如何将视图绑定与自定义视图一起使用。Google 文档仅展示了 Activity 和 Fragment。
我试过这个,但没有显示任何内容。
LayoutInflater inflater = LayoutInflater.from(getContext());
Run Code Online (Sandbox Code Playgroud)
然后,我使用了这个,但同样没有运气。
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我想也许我没有针对我的观点选择正确的布局充气器,但不确定。
片段和自定义视图可以实现类似的功能,我知道片段比自定义视图更具可重用性,使用片段的任何其他好处/增强功能?片段是否应该取代自定义视图,或者只是针对某些特定目的的增强?
例如,下面的代码是片段:
public class TestFragment extends Fragment {
private TextView tv_name;
private Button btn_play;
private Button btn_delete;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.testfragment, container, false);
}
@Override
public void onStart() {
super.onStart();
tv_name = (TextView)getView().findViewById(R.id.tv_name);
btn_play = (Button)getView().findViewById(R.id.btn_play);
btn_delete = (Button)getView().findViewById(R.id.btn_delete);
}
}
Run Code Online (Sandbox Code Playgroud)
自定义视图的代码:
public class TestCustomView extends LinearLayout {
private TextView tv_name;
private Button btn_play;
private Button btn_delete;
public TestCustomView(Context context, AttributeSet attrs){
super(context, attrs);
setOrientation(LinearLayout.HORIZONTAL);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tv_name …Run Code Online (Sandbox Code Playgroud) 我已经从eclipse导入了一个项目到Android工作室.它使用自定义视图:
xmlns:app="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)
我从activity_ro.xml文件中获得以下行中的错误,如:
"错误:(136)在'com.app.xxxx'包中找不到属性'pstsTabPaddingLeftRight'的资源标识符"
app:pstsDividerColor="#00000000"
app:pstsIndicatorColor="#FF33B5E6"
app:pstsTabPaddingLeftRight="14dip"
app:pstsUnderlineColor="#FF33B5E6" />
Run Code Online (Sandbox Code Playgroud)
我不确定自定义视图对于gradle是否有所不同,或者我做错了什么.有人有想法吗?
android ×10
android-view ×2
android-xml ×1
attr ×1
custom-view ×1
enums ×1
fragment ×1
snackbar ×1