我正在尝试设置水平滚动视图的位置,因此它与按下的按钮相对应.我尝试使用它来设置它失败:
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();
hsv.scrollTo(x, y);
这没有任何结果,滚动视图不受影响.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> …我有一个名为FractalView的类,它扩展了ImageView.我的目标是用这个具有整个屏幕大小的类来绘制分形.它是我活动的布局文件中唯一的View,而不是顶级的LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
    android:background="#FFFFFFFF">
    <com.antonc.fractal_wallpaper.FractalView
        android:id="@+id/fractal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>
如您所见,我对FractalView和LinearLayout的layout_width和layout_height都使用"match_parent".此时我假设FractalView的大小设置为某个值,因为它匹配它的父级,它匹配它的父级(这是整个屏幕).然后我尝试使用getWidth()和getHeight()检测FractalView可用的大小:
public class FractalView extends ImageView {
private Context mContext;
private Handler mHandler;
private int mScrWidth;
private int mScrHeight;
public FractalView(Context context) {
    super(context);
    mContext = context;
    init();
}
private void init() {
    mScrWidth = getWidth();
    mScrHeight = getHeight();
    // Breakpoint
}
@Override
protected void onDraw(Canvas canvas) {
    [...]
}
}
但是当我的代码到达断点时,调试器显示mScrWidth和mScrHeight为零.我尝试过使用getMeasuredWidth()和getMaxWidth(),但它们也返回了不正确的值.我已经阅读了类似问题的几个答案,即这一个,它们都解释了getWidth()在绘制后返回图像的大小,而我实际需要的是该视图可用的区域宽度,在我开始绘画之前.
有没有办法检索宽度和高度的这些值?
假设我想使用ImageView显示某些内容的图像,我想在其上面放置新视图(例如,在世界地图图像上显示图钉的动画ImageView,或智能手机图像顶部的切换视图) ).
这意味着无论ImageView如何显示图像,视图都应该在其中,在正确的位置内,具有与指定相同的大小,或者与imageView本身相关的大小
与其他视图相反,ImageView可以具有一定的大小,但其内容是其他内容(保持宽高比).
我试图使用ConstraintLayout,但这并没有真正帮助,因为ImageView可以改变它的大小(例如在更改方向时),从而破坏了我给出了与ImageView相比的视图的位置.
我发现一些库可以处理类似的事情(比如这里),我甚至在此之前问过类似的问题,但是所有这些解决方案都是针对ImageView中的静态图像,但我搜索的是添加一个视图在ImageView之上.
如何正确地将视图放在ImageView的内容之上?
与ImageView内容的大小相比,如何向下/向上缩放视图?
可以使用ConstraintLayout根据ImageView的大小更改视图的比例吗?
我想知道如何测量视图的尺寸.就我而言,它是绝对布局.我已经阅读了有关这些问题的答案,但我仍然没有得到它.
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);      
    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);       
    drawOval(); 
}
public void drawOval(){ //, int screenWidth, int screenHeight){ 
    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);     
    int screenWidth = layoutbase.getWidth();
    int screenHeight = layoutbase.getHeight();
    Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);
    Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);
    ...some code ...
    ((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));
    mybutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showText(mybutton);
        }
    });
}
public void showText(View button){      
    int …我有一个LinearLayout宽度设置为xml 的a fill_parent,现在我在运行时以编程方式需要它的宽度.所以我这样做了:
    LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
    LayoutParams layParamsGet= layoutGet.getLayoutParams();
    int width=layParamsGet.width;
但是width发现调试的价值是-1,任何有想法的人为什么不能在运行时获得LinearLayout的确切宽度,并在layout xml中设置fill_parent.
我读过的所有内容都说您不能在构造函数中调用getWidth()或getHeight()在上调用View,但我在中调用它们onResume()。届时是否应该绘制屏幕布局?
@Override
protected void onResume() {
    super.onResume();
    populateData();
}
private void populateData() {
    LinearLayout test = (LinearLayout) findViewById(R.id.myview);
    double widthpx = test.getWidth();
}
android android-layout activity-lifecycle android-view android-viewtreeobserver
如何获得可以在TextView中写入的最大行数?我试过这个:
DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager (). getDefaultDisplay (). getMetrics (metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
int lines = (int) (height / textView.getLineHeight ());
System.out.println (lines);
但是,此变体提供了错误的信息.
我有,我有一个Android的布局设计,AppBar其中将包含一个布局Toolbar和一个更LinearLayout设计有圆形Drawable的TextView.这是相同的高度.因此,当我尝试获得Toolbar或AppBar高度/宽度时,它仅返回0.
activity_main.xml中:
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="enterAlways" />
       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:layout_gravity="center_horizontal"
            android:background="@color/colorPrimary"
            android:orientation="horizontal"
            app:layout_scrollFlags="enterAlways">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="2dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/marked_questions"
                    style="@style/textview_summary_omr_toolbar_count"
                    android:background="@drawable/bg_percentage_default"
                    android:text="18" />
                <TextView
                    style="@style/textview_heading_summary_omr_toolbar"
                    android:text="Marked" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="2dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/correct"
                    style="@style/textview_summary_omr_toolbar_count"
                    android:background="@drawable/bg_percentage_6"
                    android:text="18"
                    />
                <TextView
                    style="@style/textview_heading_summary_omr_toolbar"
                    android:text="Correct" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="2dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <TextView …为什么lColorWheel.getWith()方法返回0?我想这与onMeasure事件有关,但我真的无法从文档中了解它是如何工作的.我必须为mDrawable设置尺寸?
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_light);
    ShapeDrawable mDrawable;
    ivColorWheel=(ImageView)findViewById(R.id.ivColorWheel);
    lColorWheel=findViewById(R.id.lColorWheel);
    ld=(LayerDrawable)getResources().getDrawable(R.drawable.colorwheel);  
    mDrawable = new ShapeDrawable(new OvalShape());
    mDrawable.setIntrinsicWidth(lColorWheel.getWidth());
    mDrawable.setIntrinsicHeight(lColorWheel.getHeight());
    Log.d("lColorWheel.getWidth()",Integer.toString(lColorWheel.getWidth())); //returns 0, why?
    Log.d("lColorWheel.getHeight()",Integer.toString(lColorWheel.getHeight())); //returns 0, why ?
和适当的XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:addStatesFromChildren="true" >
<RelativeLayout
    android:id="@+id/lColorWheel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true">
     <ImageView
        android:id="@+id/ivColorWheel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:src="@drawable/iconwheel"
        />
      <ImageView
          android:id="@+id/ivCenterButton1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:maxHeight="5dp"
          android:maxWidth="5dp"
          android:onClick="clc"
          android:paddingBottom="0dp"
          android:paddingLeft="0dp"
          android:paddingRight="0dp"
          android:paddingTop="0dp"
          android:src="@drawable/center3" />
</RelativeLayout>
<SeekBar
     android:id="@+id/sbColorIntensity"
     android:layout_width="match_parent"
     android:layout_height="50dp" …这是我的布局.我需要在CategoryFragment中获取宽度.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/category_list"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@color/white_background"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/pianodule_list"
            android:textSize="16sp" />
        <com.handmark.pulltorefresh.library.PullToRefreshListView
            android:id="@id/refreshable_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:descendantFocusability="beforeDescendants"
            android:divider="@null" />
    </LinearLayout>
    <View
        android:layout_width="0.1dp"
        android:layout_height="match_parent"
        android:background="@color/divider_line" />
    <fragment
        android:name="com.srefu.frag.CategoryFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="category" />
</LinearLayout>
所以在CategoryFragment的onViewCreated()中我尝试了几种方法来实现它,但一切都行不通.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View v = view;
    // View v = getView(); 
    int w1 = v.getWidth(); //return 0
    int w2 = v.getLayoutParams().width; //return NullPointerException
    int w3 = v.getMeasuredWidth(); //return …android ×10
android-view ×1
fragment ×1
imageview ×1
line ×1
measure ×1
performance ×1
position ×1
text ×1
textview ×1