我有一个包含 4-5 个子视图的 RelativeLayout 的 xml 布局。我想有一个基于此 xml 布局的自定义 View 类和一个自定义的 onclick 监听器。
我尝试通过扩展 RelativeLayout 并将视图作为成员来使用自定义类。在我的构造函数中,我正在膨胀布局并将其分配给我的 View 成员。但我想让类本身类似于我膨胀的视图对象。(我说的有道理吗!!)
我当前的代码类似于以下内容:
public class CustomItemView extends RelativeLayout {
private Context context;
private View itemView;
public CustomItemView(Context context) {
super(context);
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(layout, null);
}
public View getView() {
return itemView;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要用我的标签自定义视图,问题是FILL_PARENT不工作(如看到这里).
所以我需要使用margin和stuff,但是为了让视图在所有配置(横向/纵向,或平板电脑的高度都会改变)的选项卡中居中,这样做有点棘手.
我不知道在每个配置上使用什么值.另外,我没有找到系统用来开始的默认布局.
在我的自定义视图中,用户可以绘制一些路径,然后将这些路径与背景位图合并,并将所有内容保存在 .JPG 文件中。问题是:如果我将位图缩放 2 倍,如何对路径进行相同的缩放?
谢谢你。
我正在尝试实现自定义列表视图.它显示很好,当我慢慢滚动它时它正常工作.但是,当我开始更快地滚动时,应用程序崩溃了.下面是我的列表视图适配器的代码.
import java.util.List;
import MainActivity.ListViewItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class CustomListViewAdapter extends BaseAdapter {
LayoutInflater inflater;
List<ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个与常规WebView完全相同的自定义WebView,除了它有圆角.圆角需要透明,因为我想将此WebView放在对话框中.
我尝试制作我的自定义类:
public class RoundedWebView extends WebView
{
private Context context;
private int width;
private int height;
public RoundedWebView(Context context)
{
super(context);
initialize(context);
}
public RoundedWebView(Context context, AttributeSet attrs)
{
super(context, attrs);
initialize(context);
}
public RoundedWebView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context)
{
this.context = context;
}
// This method gets called when the view first loads, and also whenever the
// view changes. Use this opportunity to save the view's …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 a 嵌入RecyclerView自定义视图 (a RefreshableList)的正文中。
这是我的项目结构:它包含 2 个模块,app和rlist。
所述RLIST模块保持(自定义视图RefreshableList),其中我要嵌入RecyclerView。
RefreshableList.java(自定义视图)
public class RefreshableList extends RelativeLayout {
private RecyclerView mRecyclerView;
private Context mContext;
private MyAdapter mAdapter;
public RefreshableList(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
Log.i("ADAPTER", "RefreshableList is initializing views...");
setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.refreshable_list, null);
findViewsById(view);
setupRecyclerView();
}
private void findViewsById(View view) {
mRecyclerView = (RecyclerView) …Run Code Online (Sandbox Code Playgroud) 我正在尝试了解Android如何衡量和展示观点.我创建了一个包含自定义视图和视图组的示例应用.
CustomViewGroup
public class CustomViewGroup extends ViewGroup {
private static final String LOG_TAG = "CustomViewGroup";
public CustomViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(LOG_TAG, String.format(Locale.ENGLISH,
getTag() + " " +
"onMeasure(widthMeasureSpec=%d, heightMeasureSpec%d)",
widthMeasureSpec, heightMeasureSpec
));
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
// Make or work out measurements for children here …Run Code Online (Sandbox Code Playgroud) PasscodeView我的对话框片段布局中有一个自定义视图。
密码视图.java:
public class PasscodeView extends ViewGroup {
EditText mEditText;
int mDigitCount;
private int mDigitWidth;
private int mDigitRadius;
private int mOuterStrokeWidth;
private int mInnerStrokeWidth;
private int mDigitInnerRadius;
private int mDigitSpacing;
private int mDigitElevation;
private int mControlColor;
private int mHighlightedColor;
private int mInnerColor;
private int mInnerBorderColor;
private OnFocusChangeListener mOnFocusChangeListener;
private PasscodeEntryListener mPasscodeEntryListener;
public PasscodeView(Context context) {
this(context, null);
}
public PasscodeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PasscodeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); …Run Code Online (Sandbox Code Playgroud) keyboard android android-custom-view android-softkeyboard android-dialogfragment
尝试在我的应用程序中创建自定义 SurfaceView 视图时出现错误。
<com.example.test.CustomSurfaceView
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)
这是课程:
package com.example.test;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.List;
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private Context mContext;
private SurfaceHolder mHolder;
private Camera mCamera;
private List<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
public CustomSurfaceView(Context context, Context mContext) {
super(context);
this.mContext = mContext;
}
public CustomSurfaceView(Context context, Camera camera) {
super(context);
mContext = context;
mCamera = camera;
// supported preview sizes …Run Code Online (Sandbox Code Playgroud) 我做了一个自定义布局,想要为实现RadioButton。android类的代码在这里:
public class MyRadioButton extends LinearLayout implements View.OnClickListener{
private ImageView iv;
private TextView tv;
private RadioButton rb;
private View view;
public MyRadioButton(Context context) {
super(context);
view = View.inflate(context, R.layout.my_radio_button, this);
setOrientation(HORIZONTAL);
rb = (RadioButton) view.findViewById(R.id.radioButton1);
tv = (TextView) view.findViewById(R.id.textView1);
iv = (ImageView) view.findViewById(R.id.imageView1);
view.setOnClickListener(this);
rb.setOnCheckedChangeListener(null);
}
public void setImageBitmap(Bitmap bitmap) {
iv.setImageBitmap(bitmap);
}
public View getView() {
return view;
}
@Override
public void onClick(View v) {
boolean nextState = !rb.isChecked();
LinearLayout lGroup = (LinearLayout)view.getParent();
if(lGroup != null){
int …Run Code Online (Sandbox Code Playgroud)