android中的@{} @+id ?/attr

本文详细介绍了Android开发中各种资源引用的方法,包括自定义资源、系统资源、非公开资源及主题属性等的引用方式,并提供了数据绑定的相关说明。
一.@代表引用资源
1.引用自定义资源
  如:android:text="@string/apptitle"
2.引用系统资源  
  如:android:textColor="@android:color/white"
3.@+id/资源ID名   
 表示该该资源ID存在则使用该资源ID,如果不存在则创建一个新性的。如:android:id="@+id/player_controls"
二.@*代表引用系统的非公开资源,一般不这么使用。
三.?/attr代表引用主题属性
    具体实例参见 http://blog.youkuaiyun.com/happyweb/article/details/51075898
四、@{}表示数据绑定
    就是布局xml中给出一个<data>元素,在属性中可以使用@{}引用这些对象,具体参见 developer.android.com/tools/data-binding/guide.html#build_environment
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"> <!-- preference list --> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.core.widget.NestedScrollView> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <LinearLayout android:id="@+id/description_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="?attr/actionBarSize" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.5"> <TextView android:id="@+id/description_text" style="@style/PermissionAppsHeaderText" /> </LinearLayout> <LinearLayout android:id="@+id/sensor_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="?attr/actionBarSize" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.5"> <FrameLayout android:id="@+id/sensor_prefs_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="18dp" /> </LinearLayout> <androidx.appcompat.widget.Toolbar android:id="@+id/permapps_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="top" app:layout_collapseMode="pin" app:navigationIcon="@drawable/freeme_arrow_back" style="@style/PermissionToolBar"/> </com.google.android.material.appbar.CollapsingToolbarLayout> 如何让description_layout上滑到toolbar 底部时就开始折叠
最新发布
12-09
以下代码,请帮我将Textview修改为一个imageview 。谢谢 。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:didi="http://schemas.android.com/apk/res-auto" > <com.blog.demo10.NestedViewGroup android:id="@+id/dd_view_group" android:layout_width="match_parent" android:layout_height="match_parent" didi:header_id="@+id/view_bg" didi:target_id="@+id/target_layout" didi:inn_id="@+id/inner_rv" didi:header_init_top="0" didi:target_init_bottom="250"> <TextView android:id="@+id/view_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#f00" android:textSize="25sp" android:textAllCaps="false" android:text="@string/txt_nested_scroll_bg" /> <LinearLayout android:id="@+id/target_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#fff"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inner_rv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </com.blog.demo10.NestedViewGroup> <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" didi:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </RelativeLayout>
06-06
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.example.myapplication.databinding.ActivityMainBinding; import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private String currentInput = ""; private String expression = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // 设置所有按钮的点击监听 setButtonListeners(); } private void setButtonListeners() { // 所有按钮ID数组 int[] buttonIds = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide, R.id.btnDot, R.id.btnPercent, R.id.btnPlusMinus, R.id.btnClear, R.id.btnDelete, R.id.btnEquals }; View.OnClickListener listener = v -> { // 获取按钮文本(安全处理 null) String text = ((TextView) v).getText().toString(); if (v.getId() == R.id.btnClear) { currentInput = ""; expression = ""; binding.textViewHistory.setText(""); binding.editTextDisplay.setText("0"); } else if (v.getId() == R.id.btnDelete) { if (!currentInput.isEmpty()) { currentInput = currentInput.substring(0, currentInput.length() - 1); binding.editTextDisplay.setText(currentInput.isEmpty() ? "0" : currentInput); } } else if (v.getId() == R.id.btnEquals) { try { Expression exp = new ExpressionBuilder(expression.isEmpty() ? currentInput : expression).build(); double result = exp.evaluate(); long longResult = (long) result; String displayResult = (result == (double) longResult) ? String.valueOf(longResult) : String.valueOf(result); binding.textViewHistory.setText((expression.isEmpty() ? currentInput : expression) + " ="); binding.editTextDisplay.setText(displayResult); currentInput = displayResult; expression = ""; } catch (Exception e) { binding.editTextDisplay.setText("Error"); } } else if (v.getId() == R.id.btnPlusMinus) { if (!currentInput.isEmpty() && !currentInput.equals("0")) { if (currentInput.startsWith("-")) { currentInput = currentInput.substring(1); } else { currentInput = "-" + currentInput; } binding.editTextDisplay.setText(currentInput); } } else if (v.getId() == R.id.btnPercent) { if (!currentInput.isEmpty()) { double val = Double.parseDouble(currentInput) / 100; currentInput = String.valueOf(val); binding.editTextDisplay.setText(currentInput); } } else { // 处理数字和操作符 if (isOperator(text)) { if (!currentInput.isEmpty()) { expression = currentInput + text; binding.textViewHistory.setText(expression); currentInput = ""; } else if (!expression.isEmpty()) { expression = expression.replaceAll("[+\\-×&divide;]$", text); binding.textViewHistory.setText(expression); } } else { currentInput += text; binding.editTextDisplay.setText(currentInput); } } }; // 为每个按钮设置监听器 for (int id : buttonIds) { View btn = binding.getRoot().findViewById(id); // 或直接 binding.btn0 等 if (btn != null) { btn.setOnClickListener(listener); } } } private boolean isOperator(String str) { return "+".equals(str) || "-".equals(str) || "×".equals(str) || "&divide;".equals(str); } } <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F5F5F5" android:padding="8dp"> <!-- 历史表达式显示 --> <TextView android:id="@+id/textViewHistory" android:layout_width="0dp" android:layout_height="wrap_content" android:text="" android:textSize="16sp" android:textColor="#666666" android:gravity="end" android:padding="12dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 当前输入/结果显示 --> <EditText android:id="@+id/editTextDisplay" android:layout_width="0dp" android:layout_height="wrap_content" android:text="0" android:textSize="32sp" android:textColor="#000000" android:gravity="end" android:padding="16dp" android:background="@android:color/transparent" android:focusable="false" android:cursorVisible="false" app:layout_constraintTop_toBottomOf="@id/textViewHistory" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 按钮网格容器 --> <GridLayout android:layout_width="0dp" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="5" android:useDefaultMargins="true" app:layout_constraintTop_toBottomOf="@id/editTextDisplay" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <!-- 第一行:清除、正负、百分比、除法 --> <Button android:id="@+id/btnClear" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="C" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnPlusMinus" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="+/-" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnPercent" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="%" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnDivide" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="&divide;" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第二行:7,8,9,乘法 --> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="7" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="8" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="9" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnMultiply" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="×" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第三行:4,5,6,减法 --> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="4" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="5" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="6" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnSubtract" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="-" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第四行:1,2,3,加法 --> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="1" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="2" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="3" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="+" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第五行:0,小数点,删除,等于 --> <Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="2" android:layout_columnSpan="2" android:layout_margin="4dp" android:text="0" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnDot" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="." android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <ImageButton android:id="@+id/btnDelete" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:src="@drawable/ic_backspace" android:backgroundTint="#E0E0E0" android:contentDescription="Delete" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnEquals" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="=" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> </GridLayout> </androidx.constraintlayout.widget.ConstraintLayout>使用android Studio进行开发仿 Windows10 计算器 计算器是日常生活中常用的工具,下面仿写Windows10系统自带的计算器。计算器的页面由8部分组成,利用权重垂直方向分配高度,使组件占满整个屏幕,见图 4.12。第1 部分由EditText组成,重心向右。第2部分由水平方向的LinearLayout组成,内部包含6个TextVicw,水平方向6等分,注意字体颜色。第3部分由水平方向的LinearLayout组成,内部包含 Button和 ImageButton,水平方向4等分,ImageButton用于展示无法文本输出的“删除按键”,Button用于展示可以文本输出的其余按键。第4至8部分与第3部分设计的思路一致。注意采用Background 设置按钮不同的背景色。
09-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值