view related content code snippet notes

Keywords: Android xml encoding Fragment


Get the control in fragment in activity
 getFragmentManager().findFragmentById(id).getView().findViewById(id)


HolderView item view processing

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
    HolderView holderView;
    // Important to not just null check, but rather to a instanceof
    // since we might get any subclass of view here.
    if (convertView instanceof HolderView) {
        holderView = (HolderView) convertView;
    } else {
        holderView = new HolderView(mContext);
    }
    holderView.bind(new Digit(i));

    return holderView;
}

class HolderView extends FrameLayout{
        //private Context ctx;
        private View vroot;

        public HolderView(Context context) {
            super(context);
            init(context);
        }

        public HolderView(Context context, AttributeSet attrs, View vroot) {
            super(context, attrs);
            init(context);
        }

        private void init(Context context){
            vroot = LayoutInflater.from(context).inflate(R.layout.flyrecoder_item, this);
            ButterKnife.bind(this, vroot);
        }

		void bindData(FlyRecordData model){
					this.tv_fly_record_item_title.setText(model.itemTitle);
					this.tv_fly_record_title_today.setText(model.todayData);
		}			
 }

Add Unified view Processing to Display Form Content

mLayoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mViewGroup = (ViewGroup) activity
                .findViewById(android.R.id.content);

        mToastView = mLayoutInflater.inflate(R.layout.supertoast,
                mViewGroup, false);

        mMessageTextView = (TextView) mToastView
                .findViewById(R.id.message_textview);

Adding additional views to the head or bottom of the specified content view

private void setPoppyViewOnView(View view) {
		LayoutParams lp = view.getLayoutParams();
		ViewParent parent = view.getParent();
		ViewGroup group = (ViewGroup)parent;
		int index = group.indexOfChild(view);
		final FrameLayout newContainer = new FrameLayout(mActivity);
		group.removeView(view);
		group.addView(newContainer, index, lp);
		newContainer.addView(view);
		final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParams.gravity = mPoppyViewPosition == PoppyViewPosition.BOTTOM ? Gravity.BOTTOM : Gravity.TOP;
		newContainer.addView(mPoppyView, layoutParams);
		group.invalidate();
	}

public enum PoppyViewPosition {
		TOP, BOTTOM
	};
private View mPoppyView;

Set up picture overlay layer (uneven effect)

Drawable[] array = new Drawable[2];       
array[0] = getResources().getDrawable(R.drawable.qq_girl);       
array[1] = getResources().getDrawable(R.drawable.qq_boy);       
LayerDrawable la = new LayerDrawable(array);       
// The first parameter is the index number of the layer, and the last four parameters are left, top, right and bottom.
la.setLayerInset(0, 0, 0, 0, 0);       
la.setLayerInset(1, 18, 35, 180, 76);       
image.setImageDrawable(la);  

It can also be implemented in xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/compassbackground" />
    <item android:drawable="@drawable/compass_text" />
</layer-list>



Expanding the effective click area of the control (for example, imageview, which is insensitive when clicking on a smaller image) requires scaleType properties to be set here

Improvement:
<ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
       android:src="@drawable/edit"
      android:scaleType="centerInside"/>
Here we set the size of the road ImageView, which is the effective click area of ImageViwe and can change the size according to your own situation.
Note: To use src, you can't use background or the image will be stretched


When touch ing a point on the screen, determine whether the click is in a widget control:

Rect mTouchRect = new Rect();
 private boolean isTouchedView(View view, float x, float y) {
        view.getHitRect(mTouchRect);

        // by taping top or bottom padding, the list performs on click on a border item.
        // we don't add top padding here to keep behavior consistent.
      //  mTouchRect.top += mTranslateY;

      //  mTouchRect.bottom += mTranslateY + getPaddingTop();
      //  mTouchRect.left += getPaddingLeft();
      //  mTouchRect.right -= getPaddingRight();
        return mTouchRect.contains((int)x, (int)y);
    }

activity sets the background transparent getWindow (). setBackgroundDrawable (new ColorDrawable (android. graphics. Color. TRANSPARENT);.

view Background Transparency Settings
android:background
The following two methods set the background transparent: "@android: color/transparent" and "@null"

GirdView,ListView sets divide transparency, gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));


Hide the title bar and the status bar android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
Set a transparent background, modify the AndroidManifest.xml file, and add the following attributes to the corresponding Activity:
android:theme="@android:style/Theme.Translucent"
Using the system background as the background of the application, add window flags when onCreate:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
android:theme="@android:style/Theme.Dialog"


Setting ctx at dialog level does not depend on Activity
 AlertDialog mDialog=dialog.create();  
MDialog. getWindow (). setType (Windows Manager. LayoutParams. TYPE_SYSTEM_ALERT); //Set as System-Level Warning, Key

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
The Activity main window is always resized to allow room for a soft keyboard

// Forced to be horizontal screen
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
<activity android:name=".HandlerActivity" android:screenOrientation="landscape"/>  

// Forced to be vertical screen
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
<activity android:name=".HandlerActivity" android:screenOrientation="portrait"/>

Setting properties in AndroidManifest.xml prohibits recreating Activity and adds screen switching listeners.
<activity android:name=".HandlerActivity"
sdk<3.2 android:configChanges="orientation|keyboardHidden"/>
dk>3.2 android:configChanges="orientation|screenSize"




handler cleans up all message content: mHandler. removeCallbacks AndMessages (null);

// Get the position of View in the window/screen
TextView tv= (TextView) findViewById(R.id.tv);
int loc[]=new int[2];
tv.getLocationInWindow(loc);
Log.e("TAG",loc[0]+" "+loc[1]);


Get the corresponding ID according to the widget resource name:
int resID = getResources().getIdentifier("widget_id", "id",  getPackageName());
View addButton = findViewById(resID);

The isShown() method in View was previously judged by view.getVisibility() == View.VISIBLE, but it is different from this function.
That is, isShown() returns TRUE only if the view itself and all its parent containers are visible.
Normally, we call if(view.getVisibility() == View.VISIBLE) to judge the visibility of the view itself rather than the parent container.


Getting view s directly does not require mandatory transformation

public static <T> T $(View vroot,int viewID) {
        return (T) vroot.findViewById(viewID);
    }

Use:
Button takePicture = Utils.$(v, R.id.start_camera);


Open the test program by dialing the keyboard:

Registration corresponds to Receiver
<receiver android:name=".receiver.DiagnoserReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SECRET_CODE"/>
        <data android:scheme="android_secret_code" android:host="111222"/>
    </intent-filter>
</receiver>

Receiving processing:
public class DiagnoserReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if ("android.provider.Telephony.SECRET_CODE".equals(intent.getAction())) {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, Diagnoser.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
     }
}

Dial-up dial can be opened by entering ** 111222 **


Drawing sector progress loading

public void draw(Canvas canvas) {
        if (getHideWhenZero() && mLevel == 0) {
            return;
        }
        drawBar(canvas, maxLevel, getBackgroundColor());
        drawBar(canvas, mLevel, getColor());
    }
    
    private void drawBar(Canvas canvas, int level, int color) {
        Rect bounds = getBounds();
        RectF rectF = new RectF((float) (bounds.right * .4), (float) (bounds.bottom * .4),
                (float) (bounds.right * .6), (float) (bounds.bottom * .6));
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        if (level != 0)
            canvas.drawArc(rectF, 0, (float) (level * 360 / maxLevel), false, mPaint);
    }

Screen interception

/**
     * Get the current screen capture, including the status bar
     */
    public static Bitmap snapShotWithStatusBar(Activity activity){
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;
    }
 
    /**
     * Get the current screen shot, excluding the status bar
     *
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity){
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return bp;
    }

Bitmap size calculation

static int getBitmapBytes(Bitmap bitmap) {
    int result;
    if (SDK_INT >= HONEYCOMB_MR1) {
      result = BitmapHoneycombMR1.getByteCount(bitmap);
    } else {
      result = bitmap.getRowBytes() * bitmap.getHeight();
    }
    if (result < 0) {
      throw new IllegalStateException("Negative size: " + bitmap);
    }
    return result;
  }

Save and restore the current location of ListView

private void saveCurrentPosition() {
        if (mListView != null) {
            int position = mListView.getFirstVisiblePosition();
            View v = mListView.getChildAt(0);
            int top = (v == null) ? 0 : v.getTop();
            //Save position and top
        }
    }
    
    private void restorePosition() {
        if (mFolder != null && mListView != null) {
            int position = 0;//Remove saved data
            int top = 0;//Remove saved data
            mListView.setSelectionFromTop(position, top);
        }
    }

Help, about, tips on authors, HELP, etc. dialog page

	//Method 1:
 AlertDialog ad = new AlertDialog.Builder(SettingPreference.this)
	                        .setTitle(R.string.about_dlg_title)
	                        .setMessage(R.string.about_dlg_message)
	                        .setPositiveButton(getText(R.string.ok), null).create();
	                ad.show();
					//Add Link Function
	                Linkify.addLinks((TextView) ad.findViewById(android.R.id.message),
	                        Linkify.ALL);

//Method two:
//Designing an AboutDialog class to inherit from AlertDialog
public class AboutDialog extends AlertDialog {   
    public AboutDialog(Context context) {   
        super(context);   
        final View view = getLayoutInflater().inflate(R.layout.about,   
                null);   
        setButton(context.getText(R.string.close), (OnClickListener) null);   
        setIcon(R.drawable.icon_about);   
        setTitle("Program version   v1.0.0" );   
        setView(view);   
    }   

Layout file about.xml

<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content">  
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">  
   
        <TextView android:layout_height="fill_parent" 
            android:layout_width="fill_parent" android:text="@string/help_dialog_text" 
            android:padding="6dip" android:textColor="#FFFFFF" />  
    </ScrollView>  
</FrameLayout>  




Posted by lhale on Mon, 15 Apr 2019 18:06:33 -0700