我从不同的角度来看这个.基本上要点是:
我已经在XML中为一个以编程方式运行NEEDS的接口布局了一个模板,因此它将在运行期间动态填充.
这里的问题是,XML TextView有很多布局调整(有效),是必要的.但是如果我实际上在代码中设置它们,TextView甚至都不显示.
(顺便说一下,TextView嵌套在TableRow中,因此调用了权重.)我首先设计的XML模板,用作代码的参考是这样的,它工作得很好:
<TextView
android:id="@+id/txtviewWhiteBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/textview_9patch_white"
android:gravity="center"
android:text="75"
android:textColor="@android:color/black"
android:layout_margin="20dp"
android:padding="20dp"
android:textSize="40dp" />
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,完美地展示出来.当我在代码中运行相同的布局,并应用LayoutParams时,TextView消失.
这是相关的片段:
int textViewExampleID = 9001;
private TextView txtviewExample = new TextView(this);
private void buildTextView(){
LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
txtviewExample.setId(textViewExampleID);
txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
txtviewExample.setGravity(Gravity.CENTER);
txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
paramsExample.setMargins(20, 20, 20, 20);
txtviewExample.setPadding(20, 20, 20, 20);
txtviewExample.setTextSize(40);
txtviewExample.setText("customExample");
//if I comment out the following line, this TextView displays.
//if I leave it in, it doesn't display.
txtviewExample.setLayoutParams(paramsExample);
}
Run Code Online (Sandbox Code Playgroud)
我意识到LayoutParams有各种各样的可用类,我一直在玩它们所有的LinearLayout.LayoutParams,TableView.LayoutParams,RelativeLayout.LayoutParams,LayoutParams本身...无论我尝试哪一个,任何尝试调用"setLayoutParams"渲染整个TextView.我在这里搜索了论坛,但还没有找到答案.这不是那么罕见.
在单个活动内部,当定义仅在该活动中使用的组件时,以下定义之间的真正区别是什么:
Button btnPower = null;
//or
private Button btnPower = null;
//or
public Button btnPower = null;
public void somethingUsingTheButton(){
btnPower = (Button)findViewById(R.id.btnpower_id);
}
Run Code Online (Sandbox Code Playgroud)
是否有一些"引擎盖下"的约定应该被考虑(垃圾清理,内存等),这些约定会建议总是使用私有公共,如果实体本身只会在它所写的类中使用?
我有一个按钮图形,需要"按住"功能,所以我没有使用onClickListener,而是使用onTouchListener,以便应用程序可以对
MotionEvent.ACTION_DOWN,
Run Code Online (Sandbox Code Playgroud)
和
MotionEvent.ACTION_UP
Run Code Online (Sandbox Code Playgroud)
根据触发这两个事件的速度,我可以在两者之间的时间内运行"pressAndHoldHandler".
无论如何,长话短说:我在同一个应用程序中有许多"基本"按钮,不需要按住功能,所以他们使用的是onClickListener.
这些按钮中的每一个都已使用自己的XML选择器文件以图形方式自定义:
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/btn_chicken_off" />
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="@drawable/btn_chicken_s3" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="@drawable/btn_chicken_s2" />
<item
android:state_enabled="true"
android:drawable="@drawable/btn_chicken_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)
所以,这里的问题是:使用onTouchListener无法访问上面的选择器.只有onClickListener将使用自己的方法的onClick()部分拉入状态更改,因此这些"按住"按钮永远不会更改状态.对用户来说非常可怕的反馈.
我正在通过执行以下操作,在ACTION_DOWN和ACTION_UP的开关案例中强制执行上述操作:
if (action == MotionEvent.ACTION_DOWN) {
btn_chicken.setBackgroundResource(R.drawable.btn_chicken_s3);
}
else
if (action == MotionEvent.ACTION_UP) {
btn_chicken.setBackgroundResource(R.drawable.btn_chicken_off);
}
Run Code Online (Sandbox Code Playgroud)
但它似乎是一个黑客,它错过了"专注但没有压力"的阶段.
之前有人绊过这个吗?
意识到我必须担心基于网络的线程会对我的 UI 线程造成严重破坏,我经历了为我的应用程序和远程服务器之间的命令和响应的 TCP/IP 网络乒乓球创建自定义处理程序和自定义线程的整个过程。效果很好。(您将在下面的代码中看到它被引用为mainCommunicationThread(controlCommands);)
在触发该线程/处理程序之前,我从 SQL 中提取 IP 或主机名(称为 currIPstr),然后将其与一堆其他变量一起发送到线程/处理程序。不过,我需要做的一件事是确保它是 A) 有效的 IP 地址,或者 B) 主机名已解析 - 否则,甚至开始尝试都没有意义。
好吧,没问题,我只是这样称呼:
currIP = InetAddress.getByName(currIPstr).toString().split("/")[1];
Run Code Online (Sandbox Code Playgroud)
不过,我刚刚遇到的情况是,当 Gingerbread 或 StrictMode 运行时,上面的行会触发可怕的 NetworkOnMainThreadException。
是的,对 InetAddress.getByName 的简单调用就足够了。好吧,我一直在努力解决问题,所以我将整个函数移到了可运行的环境中:
private String currStatIP = null;
private String currentStatIPstr = null;
private Integer currentStatIDstr = -1;
private String currentPort = null;
private String currentLocation = null;
private String currUserName = null;
private String currPassword = null;
private Integer currStatID = -1;
public void StatControl(Cursor c, final String[] …Run Code Online (Sandbox Code Playgroud)