我必须使用一个OnGlobalLayoutListener对象,然后删除监听器,我有一个问题与我用以下代码解决的弃用方法.
protected void onCreate(Bundle savedInstanceState) {
final LinearLayout llTotal = (LinearLayout) findViewById(R.id.mmc_ll);
ViewTreeObserver vto = llTotal.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//
// mycode
//
if (Build.VERSION.SDK_INT<16) {
removeLayoutListenerPre16(llTotal.getViewTreeObserver(),this);
} else {
removeLayoutListenerPost16(llTotal.getViewTreeObserver(), this);
}
}
});
}
super.onCreate(savedInstanceState);
}
@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeGlobalOnLayoutListener(listener);
}
@TargetApi(16)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeOnGlobalLayoutListener(listener);
}
Run Code Online (Sandbox Code Playgroud)
这是对的吗?有更好的方法来处理兼容性?
使用API 10在模拟器中运行代码我在LogCat中有以下警告
04-24 09:30:12.565: I/dalvikvm(471): Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener, referenced from method com.my.project.ActivityHome.removeLayoutListenerPost16
04-24 …Run Code Online (Sandbox Code Playgroud) 我正在构建一个UI,它在XML中都是静态定义的.所有这些都在整个地方都有重量,虽然它看起来正确,但我希望看到事情确实具有正确的高度和所有.问题是无论我在哪里调用.getHeight()我的格式布局都得到了0.我在onCreate()和onStart()中都尝试过.一样.也适用于所有UI对象.任何的想法?
package com.app.conekta;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class Conekta extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout);
FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout);
Button b=(Button) findViewById(R.id.searchButton);
Log.d("CONEKTA", String.valueOf(b.getHeight()));
}
}
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" …Run Code Online (Sandbox Code Playgroud) 我需要捕获一些ImageView的绝对位置以用作放置目标.我似乎无法找到我需要将我的代码放入读取屏幕并获得Left(),Top()..位置的事件.我不希望监控每个观点的通货膨胀.必须有某种onScreenFinsihed类型的事件,但我找不到它.onMeasure和onDraw似乎在他们的工作开始时开始,我需要知道他们何时完成.TIA.
我正在尝试根据(屏幕高度 - 我的布局高度)/ list.size动态调整行的高度.
不幸的是,在onCreate方法中,布局高度返回null(当我在单击侦听器中调用它时不是这样).
还有其他方法我可以称之为吗?
我正在寻找一种衡量Android活动实际内容区域尺寸的好方法.
获取显示始终有效.简单地说是这样的:
Display display = getWindowManager().getDefaultDisplay();
Run Code Online (Sandbox Code Playgroud)
您可以获得整个屏幕的像素数.当然,这不会考虑ActionBar,状态栏或任何其他会减少活动本身可用大小的视图.
活动运行后,您可以执行以下操作:
View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
Run Code Online (Sandbox Code Playgroud)
仅获取活动内容.但是在onCreate()中执行此操作将导致宽度和高度为0,0的视图.
有没有办法在onCreate期间获得这些维度?我想应该有一种方法可以测量任何状态栏,并从总显示大小中减去,但我无法找到方法.我认为这是唯一的方法,因为内容窗口方法总是在绘制之前返回没有宽度/高度的视图.
谢谢!
我看过类似的问题并尝试了他们的解决方案,但它对我不起作用.我尝试获取imageView的宽度,但它返回0.这是代码:
public class MainActivity extends Activity {
private ImageView image1;
float centerX,centerY;
private ImageView image2;
private boolean isFirstImage = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate);
image1 = (ImageView) findViewById(R.id.ImageView01);
image2 = (ImageView) findViewById(R.id.ImageView02);
image2.setVisibility(View.GONE);
if (isFirstImage) {
applyRotation(0, 90);
isFirstImage = !isFirstImage;
}
}
private void applyRotation(float start, float end) {
centerX=image1.getWidth()/2.0f;
centerY=image1.getHeight()/2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end,centerX , centerY);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1,
image2));
if (isFirstImage)
{
image1.startAnimation(rotation);
} else { …Run Code Online (Sandbox Code Playgroud) 我以编程方式创建TextView,如下所示:
TextView mTextView = new TextView(getApplicationContext());
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((MarginLayoutParams) params).setMargins(8, 8, 0, 0);
mTextView.setText(mText);
mTextView.setLayoutParams(params);
mTextView.setPadding(2, 2, 2, 2);
mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));
tagMap.addView(mTextView);
textViewsWidth = mTextView.getWidth();
Run Code Online (Sandbox Code Playgroud)
但是mTextView.getWidth()始终返回0
如果我尝试:
mTextView.getLayoutParams().width
Run Code Online (Sandbox Code Playgroud)
它返回LayoutParams类中的LayoutParams对应值(-1或-2)
如何获得视图的宽度?
编辑我需要在这里这样做:
@Override
public void onPostExecute(Hashtable<String, Integer> hash){
final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
final Enumeration<String> e = hash.keys();
int index = 0;
int textViewsWidth = 0;
while(e.hasMoreElements()){
final TextView tV = new TextView(getApplicationContext()); …Run Code Online (Sandbox Code Playgroud) 我正在开发一款小型Android应用.我需要这个Android应用程序的一部分是有一个水平和垂直滚动的网格.但是,最左边的列需要冻结(始终在屏幕上,而不是水平滚动的一部分).同样,顶部标题行需要冻结(不是垂直滚动的一部分)
如果以上内容没有太大意义,这张图片将有希望清楚地描述:

关键:
要做到这些尺寸中的一个很容易,我已经这样做了.但是,我无法让这两个维度发挥作用.(也就是说,我可以将底部部分全部变为蓝色,或者我可以将正确的部分全部变为红色,但不完全如上所述)我的代码如下所示,基本上会产生以下内容:

result_grid.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lightGrey">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/summaryTableLayout"
android:layout_weight="0.1"
android:layout_marginBottom="50dip"
android:minHeight="100dip">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="@+id/frozenTable"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginLeft="1dip"
android:stretchColumns="1"
/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/frozenTable"
android:layout_marginTop="2dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="1dip">
<TableLayout
android:id="@+id/contentTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="0.1"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/backButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Return"/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Java代码:
private boolean showSummaries;
private TableLayout summaryTable;
private TableLayout frozenTable;
private …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码......
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="AMIT KUMAR" />
Run Code Online (Sandbox Code Playgroud)
这是一个java代码.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
Log.d("left", textView.getLeft() + " " + textView.getRight() + " "
+ textView.getWidth()+" "+textView.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
对于以上所有我得到输出0.
那我怎样才能得到textView的宽度和高度.
我正在尝试设置水平滚动视图的位置,因此它与按下的按钮相对应.我尝试使用它来设置它失败:
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();
hsv.scrollTo(x, y);
Run Code Online (Sandbox Code Playgroud)
这没有任何结果,滚动视图不受影响.xml:
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@null"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:text="btn0"
android:id="@+id/btn0"
android:background="@drawable/yellow_btn" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="bnt1"
android:id="@+id/btn1" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn2"
android:id="@+id/btn2" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn3"
android:id="@+id/btn3" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn4"
android:id="@+id/btn4" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn5"
android:id="@+id/btn5" />
</LinearLayout> …Run Code Online (Sandbox Code Playgroud)