这是我的布局.我需要在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>
Run Code Online (Sandbox Code Playgroud)
所以在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 …Run Code Online (Sandbox Code Playgroud) 我试图获得DialogFragment运行时的宽度,我尝试了两种失败的方法.
第一:onCreateDialog创建对话框后的内部
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
...
...
AlertDialog dialog = builder.create();
width = dialog.getWindow().getAttributes().width;
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
width 设置为-1
第二:
public void onStart() {
width = getDialog().getWindow().getAttributes().width;
}
Run Code Online (Sandbox Code Playgroud)
width 设为-2
我们为什么使用ViewTreeObserver,任何人都可以解释一下?
在下面的代码creditsView是TextView对象。通过整个代码,我了解到“这是根据条件隐藏一些文本”,但是唯一的原因就是我们为什么要使用它ViewTreeObserver?
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = mainLayout.getRootView().getHeight() - mainLayout.getHeight();
if (heightDiff > 100) {
Utils.appLogger("MyActivity", "keyboard opened");
creditsView.setVisibility(View.GONE);
}
if (heightDiff < 100) {
Utils.appLogger("MyActivity", "keyboard closed");
creditsView.setVisibility(View.VISIBLE);
}
}
});
Run Code Online (Sandbox Code Playgroud) java android android-layout android-view android-viewtreeobserver
我使用布局权重将TextViews设置为线性布局.
linearLayout.setWeightSum(7f);
for(int i=0;i<7;i++){
TextView tv= new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 1f));
tv.setText("sometext");
linearLayout.addView(tv);
}
Run Code Online (Sandbox Code Playgroud)
在绘制布局后如何获得这些TextView的宽度和高度?
tv.getWidth()和tv.getHeight()返回0;
我有一个LinearLayout,这个LinearLayout将保存动态放置的视图.我需要找出LinearLayout子元素的宽度,但是必须在onCreate方法中完成.从研究中我发现你不能使用这种方法的getWidth.所以我使用onWindowFocusChanged,它适用于父LinearLayout(返回实际大小),但它不适用于它的子节点.
我注意到的另一件事是,当屏幕消失并且屏幕被锁定时,我可以在日志中看到返回的孩子的实际宽度(我认为活动正在暂停).
我真的被卡住了,这是必要的,因为我需要根据子宽度动态放置这些视图.
我在项目中使用ConstraintLayout。当我尝试在onCreate中调用getWidth时发现一个问题。结果为0。
代码在这里。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnTouchListener(new ImageViewTouchListener());
System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
xml内容。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_red"
android:background="#ff0000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_green"
android:background="#00ff00"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/btn_red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_blue"
app:layout_constraintLeft_toRightOf="@+id/btn_green"
android:background="#0000ff"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
tools:layout_editor_absoluteY="55dp"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintGuide_percent="0.09784412" />
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/sb_stroke"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/btn_green" …Run Code Online (Sandbox Code Playgroud) 我想在一些Android设备中获取我的布局或视图(不是屏幕尺寸)宽度,所以我该怎么做?例如:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:id="@+id/layout_scrol_left"/>
Run Code Online (Sandbox Code Playgroud)
我有这个布局,我想在运行时获得布局宽度和高度尺寸(在倾角),我该怎么做?