linearlayout test

本文解析了一个使用XML定义的LinearLayout布局代码,展示了如何通过Android的XML语法创建包含多个TextView和ImageView组件的水平布局。详细介绍了各组件的属性设置,如id、布局宽度、高度、对齐方式等。

<?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:orientation="horizontal" >

    <ImageView
  android:id="@+id/book_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
  
 <TextView
  android:id="@+id/order_Id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" />
 
 <TextView
  android:id="@+id/book_customer"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" />
 
 <TextView
  android:id="@+id/book_phone"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" />
   
 <TextView
  android:id="@+id/book_isopenned"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:gravity="right"
  android:layout_marginRight="1dp" />
 
  <TextView
  android:id="@+id/inventory_Id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" />
 
 <TextView
  android:id="@+id/inventory_orderdate"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" />
 
 <TextView
  android:id="@+id/inventory_days"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dip" /> 
 
</LinearLayout>

转载于:https://my.oschina.net/u/3428739/blog/882382

package com.wesing.applauncher.service; import android.annotation.SuppressLint; import android.app.Service; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.net.wifi.WifiManager; import android.os.Build; import android.os.IBinder; import android.provider.Settings; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.LinearLayout; import androidx.annotation.NonNull; import com.wesing.applauncher.R; import com.wesing.applauncher.common.AudioSource; import com.wesing.applauncher.common.MicEQMode; import com.wesing.applauncher.common.MusicEQMode; import com.wesing.applauncher.databinding.NavbarLayoutBinding; import com.wesing.applauncher.tuning.DspManager; import com.wesing.applauncher.ui.custom.BrightnessControlView; import com.wesing.applauncher.ui.custom.FocusTouchTextView; import com.wesing.applauncher.ui.custom.TimeDateView; import com.wesing.applauncher.ui.custom.VolumeControlView; import com.wesing.applauncher.util.LogTool; import com.wesing.applauncher.util.Utils; import com.wesing.ktvlauncher.control.TimeControl; import com.wesing.ktvlauncher.util.SystemIntentLauncher; import java.util.Arrays; import java.util.List; import kotlin.Unit; public class LauncherService extends Service implements TimeControl.TimeListener, View.OnTouchListener { private static final String TAG = "LauncherService"; private BrightnessControlView brightnessControl; private DspManager dspManager; private LinearLayout test; private WindowManager.LayoutParams mLayoutParams; private WindowManager mWindowManager; private FrameLayout.LayoutParams layoutParams; private FrameLayout mSourLayout; private float moveY; private float pressY; private float startY; private float endY; private boolean isDisplay; //标识下拉栏是否显示 private float mDensity; private int screenWidth; private int screenHeight; private int mHiddenViewMeasuredHeight; private List<FocusTouchTextView> touchViews; private List<FocusTouchTextView> musicViews; private List<FocusTouchTextView> micViews; private List<FocusTouchTextView> audioViews; private List<FocusTouchTextView> btViews; private TimeDateView timeLayout; private NavbarLayoutBinding binding; private VolumeControlView volumeControlView; private View view; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate: -----"); createView(); TimeControl.INSTANCE.registerListener(this); dspManager = DspManager.getInstance(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand: -----"); displayConfig(); displayMusic(); MicEQMode micEQMode = dspManager.getMicEQMode(); if (micEQMode != null) { switch (micEQMode) { case PRESET1: refreshView3(0); break; case PRESET2: refreshView3(2); break; case PRESET3: refreshView3(3); break; } } setVolumeView(); return START_NOT_STICKY; } private void setVolumeView() { int musicVolume = dspManager.getMusicVolume(); int micVolume = dspManager.getMicVolume(); int effectVolume = dspManager.getEffectVolume(); int instrumentVolume = dspManager.getInstrumentVolume(); volumeControlView.setProgress(0, Utils.convertVolume(musicVolume, 0)); volumeControlView.setProgress(1, Utils.convertVolume(micVolume, 1)); volumeControlView.setProgress(2, Utils.convertVolume(effectVolume, 2)); volumeControlView.setProgress(3, Utils.convertVolume(instrumentVolume, 3)); } private void displayMusic() { MusicEQMode eqMode = dspManager.getMusicEQMode(); if (eqMode == null) { return; } switch (eqMode) { case MUSIC: refreshView2(2); break; case CINEMA: refreshView2(1); break; case STANDARD: refreshView2(0); break; } } private void displayConfig() { AudioSource source = dspManager.getAudioSource(); if (source == null) { return; } switch (source) { case AUX: refreshView4(1); break; case LOCAL: refreshView4(0); break; case BLUETOOTH: refreshView4(2); break; } } @Override public void onDestroy() { Log.d(TAG, "onDestroy: -----"); TimeControl.INSTANCE.unregisterListener(this); dspManager.destroyUdp(); if (view != null) { mWindowManager.removeView(view); } super.onDestroy(); } @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException("Not yet implemented"); } private void createView() { binding = NavbarLayoutBinding.inflate(LayoutInflater.from(this)); view = binding.getRoot(); mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); Display display = mWindowManager.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getRealMetrics(metrics); screenWidth = metrics.widthPixels; screenHeight = metrics.heightPixels; mLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else { mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; } mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; mLayoutParams.format = PixelFormat.TRANSPARENT; mWindowManager.addView(view, mLayoutParams); initView(); initId(); initOnTouch(); } private void initId() { String id = "设备ID: " + Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); timeLayout.updateDeviceId(id); } private void initOnTouch() { for (int i = 0; i < touchViews.size(); i++) { musicViews.get(i).setOnTouchListener(this); micViews.get(i).setOnTouchListener(this); touchViews.get(i).setOnTouchListener(this); } for (FocusTouchTextView btView : btViews) { btView.setOnTouchListener(this); } for (FocusTouchTextView audioView : audioViews) { audioView.setOnTouchListener(this); } } @SuppressLint("ClickableViewAccessibility") private void initView() { timeLayout = binding.timeLayout; test = binding.test; mSourLayout = binding.sour; volumeControlView = binding.volumeControl; brightnessControl = binding.brightnessControl; layoutParams = (FrameLayout.LayoutParams) mSourLayout.getLayoutParams(); mSourLayout.setVisibility(View.GONE); mDensity = getResources().getDisplayMetrics().density; touchViews = Arrays.asList(binding.setText, binding.wifiText, binding.yuYinText, binding.xzText, binding.qlText); musicViews = Arrays.asList(binding.standardText, binding.popularityText, binding.rockText, binding.jazzText, binding.classicalText); audioViews = Arrays.asList(binding.machineText, binding.auxText, binding.bluText); micViews = Arrays.asList(binding.ktvText, binding.superstarText, binding.hostText, binding.majorText, binding.singerText); btViews = Arrays.asList(binding.preText, binding.playStatusText, binding.nextText, binding.discText); final float[] jilu = {-1}; final boolean[] isUp = {false}; mSourLayout.setOnTouchListener((v, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: pressY = event.getY(); startY = event.getY(); endY = event.getY(); LogTool.info(TAG, "pressY" + pressY + "startY" + startY + "endY---" + endY); break; case MotionEvent.ACTION_MOVE: //该处逻辑处理重点为在setLayoutParams之后moveY的变化 moveY = event.getY(); jilu[0] = pressY - moveY; if (startY > moveY) { mSourLayout.scrollBy(0, (int) jilu[0]); } else { mSourLayout.scrollBy(0, (int) jilu[0]); mSourLayout.scrollBy(0, -mSourLayout.getScrollY()); } pressY = moveY; if (endY == 0) { endY = moveY; } else if (moveY - endY > 20) { isUp[0] = false; endY = moveY; } else if (moveY - endY < 0) { isUp[0] = true; endY = moveY; } break; case MotionEvent.ACTION_UP: if (startY - endY > 8) { if (isUp[0]) { animateClose(mSourLayout); jilu[0] = -1; isUp[0] = false; }/* else { mSourLayout.scrollBy(0, -mSourLayout.getScrollY()); }*/ // } else { // mSourLayout.scrollBy(0, -mSourLayout.getScrollY()); // } } startY = 0; moveY = 0; pressY = 0; endY = 0; break; } return true; }); test.setOnTouchListener((v, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: pressY = event.getY(); break; case MotionEvent.ACTION_MOVE: moveY = event.getY(); if (moveY - pressY > 10) { animateOpen(moveY); } break; case MotionEvent.ACTION_UP: if (moveY > pressY) { mHiddenViewMeasuredHeight = (int) (mDensity * 800 + 0.5); animateOpen(mSourLayout); } else { animateClose(mSourLayout); } break; default: break; } return true; }); int screenBrightness = getScreenBrightness(); brightnessControl.setProgress(screenBrightness); brightnessControl.setOnProgressChangeListener(progress -> { boolean success = setScreenBrightness(progress); if (success) { Log.d(TAG, "屏幕亮度设置成功,新亮度值: " + progress); } else { Log.e(TAG, "屏幕亮度设置失败"); } return Unit.INSTANCE; }); volumeControlView.setOnProgressChangeListener((groupIndex, progress) -> { LogTool.info(TAG, "回调进度值----" + progress + "当前拖动的进度索引---" + groupIndex); switch (groupIndex) { case 0: int volume = (int) (progress / 11.25f); LogTool.info(TAG, "转换后的音乐音量数值" + volume); dspManager.setMusicVolume(volume); break; case 1: case 2: case 3: int micVolume = Math.round(progress * 32f / 225f); micVolume = Math.max(0, Math.min(32, micVolume)); if (groupIndex == 1) { dspManager.setMicVolume(micVolume); } else if (groupIndex == 2) { dspManager.setReverbVolume(micVolume); } else { dspManager.setInstrumentVolume(micVolume); } break; } return Unit.INSTANCE; } ); } private void animateClose(View view) { Log.i(TAG, "关闭动画"); isDisplay = false; mSourLayout.setVisibility(View.GONE); test.setVisibility(View.VISIBLE); mSourLayout.scrollBy(0, -mSourLayout.getScrollY()); mLayoutParams.height = 15; mWindowManager.updateViewLayout(binding.getRoot(), mLayoutParams); } private void animateOpen(float x) { Log.i(TAG, "animateOpen: " + x + "||" + (-screenHeight + x)); Log.i(TAG, "打开动画"); test.setVisibility(View.GONE); layoutParams.setMargins(0, 0, 0, 0); layoutParams.height = (int) screenHeight; mSourLayout.setLayoutParams(layoutParams); if (mLayoutParams.height != screenHeight) { mLayoutParams.height = screenHeight; mWindowManager.updateViewLayout(binding.getRoot(), mLayoutParams); } mSourLayout.setVisibility(View.VISIBLE); } private void animateOpen(View v) { Log.i(TAG, "打开动画2"); isDisplay = true; test.setVisibility(View.GONE); mLayoutParams.height = screenHeight; mWindowManager.updateViewLayout(binding.getRoot(), mLayoutParams); layoutParams.setMargins(0, 0, 0, 0); layoutParams.height = (int) screenHeight; v.setLayoutParams(layoutParams); v.setVisibility(View.VISIBLE); v.setEnabled(true); } @Override public void onTimeChanged(@NonNull String time) { if (timeLayout != null) { timeLayout.updateTime(time); } } @Override public void onDateChanged(@NonNull String date) { if (timeLayout != null) { timeLayout.updateDate(date); } } @Override public void onValidityUpdate(boolean valid) { } //打开wifi private boolean openWifi() { boolean bool = true; WifiManager wifiManager = ((WifiManager) getApplicationContext().getSystemService( Context.WIFI_SERVICE)); if (!wifiManager.isWifiEnabled()) { bool = wifiManager.setWifiEnabled(true); } return bool; } //打开设置 private void openSetting() { Log.d(TAG, "openSetting: -----"); SystemIntentLauncher.INSTANCE.openSettings(this); } // 获取当前屏幕亮度值(0-255) public int getScreenBrightness() { int brightness = 0; try { ContentResolver resolver = getContentResolver(); brightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Settings.SettingNotFoundException e) { Log.e(TAG, "获取屏幕亮度失败: " + e.getMessage()); e.printStackTrace(); } return brightness; } // 设置屏幕亮度(value范围:0-255) public boolean setScreenBrightness(int value) { try { // 关闭自动亮度调节 Settings.System.putInt( getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL ); // 设置亮度值 ContentResolver resolver = getContentResolver(); boolean success = Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, value); // 通知系统亮度已更改(需要在Activity中调用,Service中可能无效) // Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); // resolver.notifyChange(uri, null); return success; } catch (Exception e) { Log.e(TAG, "设置屏幕亮度失败: " + e.getMessage()); e.printStackTrace(); return false; } } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { switch (v.getId()) { case R.id.set_text: refreshView(0); openSetting(); animateClose(mSourLayout); break; case R.id.wifi_text: openWifi(); refreshView(1); break; case R.id.yu_yin_text: refreshView(2); break; case R.id.xz_text: refreshView(3); break; case R.id.ql_text: refreshView(4); break; case R.id.standard_text: refreshView2(0); setMusicEffect(MusicEQMode.STANDARD); break; case R.id.popularity_text: refreshView2(1); setMusicEffect(MusicEQMode.CINEMA); break; case R.id.rock_text: refreshView2(2); setMusicEffect(MusicEQMode.MUSIC); break; case R.id.jazz_text: refreshView2(3); break; case R.id.classical_text: refreshView2(4); break; case R.id.ktv_text: refreshView3(0); setMicMode(MicEQMode.PRESET1); break; case R.id.superstar_text: refreshView3(1); break; case R.id.host_text: refreshView3(2); setMicMode(MicEQMode.PRESET2); break; case R.id.major_text: refreshView3(3); setMicMode(MicEQMode.PRESET3); break; case R.id.singer_text: refreshView3(4); break; case R.id.machine_text: refreshView4(0); setAudioSource(AudioSource.LOCAL); break; case R.id.aux_text: refreshView4(1); setAudioSource(AudioSource.AUX); break; case R.id.blu_text: refreshView4(2); setAudioSource(AudioSource.BLUETOOTH); break; } } return true; } private void setAudioSource(AudioSource audioSource) { if (audioSource != null) { dspManager.setAudioSource(audioSource); } } private void setMusicEffect(MusicEQMode musicEQMode) { if (musicEQMode != null) { dspManager.setMusicEqMode(musicEQMode); } } private void setMicMode(MicEQMode micMode) { if (micMode != null) { dspManager.setMicEQMode(micMode); } } private void refreshView(int index) { for (int i = 0; i < touchViews.size(); i++) { if (i == index) { touchViews.get(index).setBackgroundResource(R.drawable.bg2_press); } else { touchViews.get(i).setBackgroundResource(R.drawable.bg2); } } } private void refreshView2(int index) { for (int i = 0; i < musicViews.size(); i++) { if (i == index) { musicViews.get(index).setBackgroundResource(R.drawable.bg1_press); } else { musicViews.get(i).setBackgroundResource(R.drawable.bg1); } } } private void refreshView3(int index) { for (int i = 0; i < micViews.size(); i++) { if (i == index) { micViews.get(index).setBackgroundResource(R.drawable.bg1_press); } else { micViews.get(i).setBackgroundResource(R.drawable.bg1); } } } private void refreshView4(int index) { for (int i = 0; i < audioViews.size(); i++) { if (i == index) { audioViews.get(index).setBackgroundResource(R.drawable.bg3_press); } else { audioViews.get(i).setBackgroundResource(R.drawable.bg3); } } } private void refreshView5(int index) { for (int i = 0; i < btViews.size(); i++) { if (i == index) { btViews.get(index).setBackgroundResource(R.drawable.bg1_press); } else { btViews.get(i).setBackgroundResource(R.drawable.bg1); } } } }哪里引起activity第一次起来后导致ViewPager2无法滑动
08-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值