我有一个ScrollView持有一系列的Views.我希望能够确定一个视图当前是否可见(如果当前显示它的任何部分ScrollView).我希望下面的代码可以做到这一点,令人惊讶的是它没有:
Rect bounds = new Rect();
view.getDrawingRect(bounds);
Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(),
scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight());
if(Rect.intersects(scrollBounds, bounds))
{
//is visible
}
Run Code Online (Sandbox Code Playgroud) 我以编程方式将自定义视图添加到垂直LinearLayout,我希望视图之间有一些空间.我尝试添加:setPadding(0,1,0,1)到我的CustomView构造函数,但这似乎没有任何影响.有什么建议?
*有人指出我应该使用保证金.由于我是动态添加视图,因此我需要从代码中设置边距(而不是在xml中).我相信这样做的方法如下,但它不起作用.
public class MyView extends View
{
public MyView (Context context)
{
super(context);
MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 10);
setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
*编辑.我还尝试使用MarginLayoutParams作为参数,同时将视图添加到线性布局(如下所示).这也行不通:
MarginLayoutParams params = new MarginLayoutParams(linearLayout.getLayoutParams());
linearLayout.setMargins(0, 10, 0, 10);
linearLayout.addView(view, params);
Run Code Online (Sandbox Code Playgroud)
谢谢.
这似乎很简单,但我无法弄清楚如何做到这一点.我有一个带有EditText和两个ImageButtons的水平布局.我希望ImageButtons具有固定大小,并且EditText占用布局中的剩余空间.如何实现这一目标?
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<ImageButton
android:src="@drawable/img1"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
<ImageButton
android:src="@drawable/img2"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我想在代码中检索textApperanceLarge的int值.我相信下面的代码是正确的方向,但无法弄清楚如何从TypedValue中提取int值.
TypedValue typedValue = new TypedValue();
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);
Run Code Online (Sandbox Code Playgroud) 我想创建一个EditText,它只接受数字,最多有4个数字,并且永远不会比填充4个数字时更窄(空时不缩小,或者必须扩展以容纳4个数字,所以它是固定.)
前两个完成:android:inputType ="number"和android:maxLength ="4".
如何设置最小长度?
我ListView正在使用扩展名BaseAdapter,我无法正确刷新它.当我刷新时,似乎旧数据在新数据之上绘制,直到滚动事件发生.旧行绘制在新行的顶部,但是当我开始滚动时,旧行会消失.
我已经打过电话invalidateViews(),notifyDataSetChanged()和notifyDataSetInvalidated().我的代码看起来像:
private void updateData()
{
List<DataItems> newList = getNewList();
MyAdapter adapter = new MyAdapter(getContext());
//my adapter holds an internal list of DataItems
adapter.setList(newList);
mList.setAdapter(adapter);
adapter.notifyDataSetChanged();
mList.invalidateViews();
}
Run Code Online (Sandbox Code Playgroud) 我想实现一个显示对话框的方法,等待对话框解除,然后根据对话框内容返回结果.这可能吗?
public String getUserInput()
{
//do something to show dialog
String input = //get input from dialog
return input;
}
Run Code Online (Sandbox Code Playgroud)
我实际上是在尝试实现一个具有方法"public String getUserInput()"的接口,其中必须通过对话框检索返回的String.这很容易在java中完成,在android中似乎不可能?
编辑:根据评论中的要求发布一些示例代码
getInput()必须从后台线程调用(我从AsynchTask中调用它).getInput()显示一个对话框并调用wait.在对话框上按下确定按钮时,对话框在成员变量中设置用户输入并调用notify.调用notify时,getInput()继续并返回成员变量.
String m_Input;
public synchronized String getInput()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
//customize alert dialog to allow desired input
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
m_Input = alert.getCustomInput();
notify();
}
});
alert.show();
}
});
try
{
wait();
}
catch (InterruptedException e) …Run Code Online (Sandbox Code Playgroud) 在Java中我会做类似的事情:
java.awt.GraphicsEnvironment ge =
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
Run Code Online (Sandbox Code Playgroud)
是否有Android等价物?
下面是我检查剩余内存量的公式(不是当前堆中剩余多少内存,而是在应用程序崩溃之前可以使用多少内存).我不是很确定这是对的,是吗?
double max = Runtime.getRuntime().maxMemory(); //the maximum memory the app can use
double heapSize = Runtime.getRuntime().totalMemory(); //current heap size
double heapRemaining = Runtime.getRuntime().freeMemory(); //amount available in heap
double nativeUsage = Debug.getNativeHeapAllocatedSize(); //is this right? I only want to account for native memory that my app is being "charged" for. Is this the proper way to account for that?
//heapSize - heapRemaining = heapUsed + nativeUsage = totalUsage
double remaining = max - (heapSize - heapRemaininng + nativeUsage);
Run Code Online (Sandbox Code Playgroud) 我想use intent.setData(Uri uri)传递从URL获得的数据.为了做到这一点,我需要能够从URL创建一个Uri(或者从我从URL读取的byte [],或者ByteArrayInputStream我从中创建的byte[]等等).但是,我无法弄清楚应该怎么做.
那么,无论如何从URL获取的数据创建Uri而不首先将数据写入本地文件?