基本上我有这个内部XML,但我必须在代码中重新创建它.我该怎么做?
<EditText
android:layout_width="140dp"
android:layout_height="20dp"
android:background="@drawable/input_bg01"
android:textSize="10dp"
android:gravity="center"
android:text="111-222-333 FOOO" />
Run Code Online (Sandbox Code Playgroud)
我可以使用它来设置文本大小,但layout_width和height呢?
edTxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉代码使用DP单元而不是像素?或转换功能将DP转换为像素?
如上图所示.此屏幕截图有三种视图.
- 第一项是CheckBox
文本并关闭状态.
- 第二项CheckBox
没有文字,状态开启.
- 最后一项是ImageView
src指向可绘制图像.
CheckBoxes是使用自定义的android:button
.
当我尝试使用较小的图像时,所有复选框都是左对齐的.
比较这两个图像告诉我,默认大小CheckBox
似乎固定为一定大小,直到文本属性足够大,需要扩展.
文件中没有任何特殊内容.见下文.
custom_cb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/creditcard_selected" android:state_checked="true" />
<item android:drawable="@drawable/creditcard"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
layout.xml
<?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">
<CheckBox android:id="@+id/cbFalse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:text="" />
<CheckBox android:id="@+id/cbTrue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:focusable="false"
android:checked="true"
android:layout_toRightOf="@id/cbFalse" />
<ImageView android:id="@+id/imvTrue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/creditcard"
android:layout_toRightOf="@id/cbTrue" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以使用更大的图像CheckBox同时保持大小wrap_content
?如果我将CheckBox设置layout_width
为实际像素或dp,则显示完整图像,但这意味着每次更改时都必须手动检查大小.
我试着搜索它,但我不能.我在联想G460笔记本电脑上运行Windows 7.我尝试在其上安装Ubuntu但由于某种原因我无法使用无线连接到互联网.
无论如何我有机会看看Android源代码或至少只是它的日历部分吗?
我正在研究基于Galaxy S的应用程序.我知道Galaxy S宽480像素,高800像素,但在DP中多少钱?
让我们说如果我想要并排放置两个布局,我会将它们设置为240像素.但我怎么知道我应该在DP单元中使用什么值?
我正在为一个项目制作几个Android库应用程序.为了简化问题,假设我在这个项目中有两个库(utilLib,screenLib)(现在将被称为app).
每个项目中都有相同名称的String资源,但值不同.像这样:
utilLib
<string name="app_version">1.0</string>
<string name="hello">UtilLib Hello</string>
Run Code Online (Sandbox Code Playgroud)
screenLib
<string name="app_version">0.7a</string>
<string name="hello">ScreenLib Hello</string>
Run Code Online (Sandbox Code Playgroud)
应用
<string name="app_version">0.1</string>
Run Code Online (Sandbox Code Playgroud)
我意识到我可以使用com.package.R来引用该字符串,但是如果我的代码看起来会出现什么呢?
<!-- language: java -->
import com.app.R;
...
private void checkValue(){
String version = getString(R.app_version);
Log.d(TAG, "version: " + version); // 0.1 show be here
String hello = getString(R.hello);
Log.d(TAG, "Hello: " + hello); // <---- ? ('UtilLib Hello' or 'ScreenLib Hello')
}
Run Code Online (Sandbox Code Playgroud)
我试图在这里进行模块化构建,但不完全理解Android如何优先使用它的R.java.有没有人经历过这个?
所以我有一个DialogFragment显示区域选择列表.用户单击该区域后,列表将重新获取数据以显示街道选择列表.此时,我想让用户按下硬件返回按钮返回区域选择.这可能吗?我试图覆盖下面的一些方法,但我只能点击事件,但不能阻止它发生.
@Override
public void onCancel(DialogInterface dialog) {
if(isDismissable()){
super.onCancel(dialog);
}else {
Log.d(TAG, "Don't dismiss cancel this dialog!");
}
}
@Override
public void dismissAllowingStateLoss() {
if(isDismissable()){
super.dismissAllowingStateLoss();
}else {
Log.d(TAG, "Don't dismiss this dialog!");
}
}
@Override
public void dismiss() {
if(isDismissable()){
super.dismiss();
}else {
Log.d(TAG, "Don't dismiss this dialog!");
}
}
Run Code Online (Sandbox Code Playgroud)
dismiss()
当用户按下后退按钮时调用super.dismiss()
,但即使我不调用,对话也会被解雇.
有没有办法做到这一点?我还会查看Google+应用ActionBar
在DialogFragment中的显示方式,HomeAsUp
但我找不到任何相关信息.
我的目标:我试图在仍然停留在屏幕上的同时点击屏幕上的一个项目时播放视频。
因此,我求助于PopupWindow,但是以某种方式VideoView不会出现在弹出窗口中。
我可以很好地显示弹出窗口,但VideoView不能显示(实际上,它不是不可见,而是冻结屏幕的该部分)。如果查看第二个和第三个屏幕,您会看到不可见区域不是完全矩形。那是因为我正在为popupwindow设置动画。
我在另一个Activity中检查了VideoView本身,并且播放效果很好。我使用Nexus One和Galaxy S进行了测试,两者均显示相同的结果。
在StackOverflow上的快速搜索显示了以下问题:android视频,听到声音但没有视频 导致 Android无法播放Video .mp4, 并且两者都不对我有用。
同样,如您在第三个屏幕上看到的,MediaController并不完全将自身附加到视频或popupwindow上,而是附加到Activity上。
这是屏幕
所以我有自定义列表项,带有ListView按钮.按下时,按钮显示交替可绘制以向用户显示反馈.但是,当我点击该行时,每个按钮都显示按下状态,就像我点击它们一样.
如何让按钮显示其原始状态而不是state_pressed?
布局/列表项目:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical|left" >
<TextView
android:id="@+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
style="@style/PrimaryText" />
<TextView
android:id="@+id/txtSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
style="@style/SecondaryText" />
</LinearLayout>
<ImageButton
android:id="@+id/imbResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:focusable="false"
android:duplicateParentState="false"
android:src="@drawable/response_btn"
android:contentDescription="@string/response"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
绘制/ response_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/res_m" />
<item android:state_pressed="true" android:drawable="@drawable/res_m" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/res_alt_m" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我试图删除state_focused和state_pressed,state_focused.似乎该按钮从其父级获取state_pressed.
我希望有一个ListView,当我点击该项目时,视图向左滑动.
所以我有:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
arg1.startAnimation(openAnimation);
}
});
Run Code Online (Sandbox Code Playgroud)
但是,动画应用于列表中的不同项而不是单击的项.这种行为似乎是随机的,因为它有时同时发生在多个项目上.
我怀疑这是因为适配器重用它的方式来创建项目.我去修改我的适配器中的getView方法,以便在每次调用时对新视图进行充气.然后动画不再发生了.
有办法解决这个问题吗?我试图将动画移动到我的适配器内,但后来我无法将它与Listview上的其他操作相关联.
最终,我希望该项目是可点击的,但是当向左/向右滑动显示删除按钮(iOS删除行为)时.我在这里走错了路吗?这应该是可行的,因为Android可以在通知栏中实现滑动删除.
我有一个处理片段创建的控制器类.让我们说如下:
public class FragmentController {
public static Fragment newInstance(String title, int total) {
return total > 0? MultipleDataFragment.newInstance(title, total)
: SingleDataFragment.newInstance(title);
}
}
Run Code Online (Sandbox Code Playgroud)
public class MultipleDataFragment extends Fragment {
public static MultipleDataFragment newInstance( String title, int total) {
Bundle b = new Bundle();
b.putString("title", title);
b.putInt("total", total);
}
}
Run Code Online (Sandbox Code Playgroud)
public class SingleDataFragment extends Fragment {
public static SingleDataFragment newInstance( String title, int total) {
Bundle b = new Bundle();
b.putString("title", title);
b.putInt("total", total);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试(标准Junit4测试类)中,我有:
@Test
public void testNewInstanceCreteMultipleData() …
Run Code Online (Sandbox Code Playgroud) android ×10
java ×2
listview ×2
animation ×1
checkbox ×1
highlight ×1
layout ×1
listitem ×1
modularity ×1
popupwindow ×1
r.java-file ×1
unit-testing ×1