我不想使用普通或迷你尺寸的FAB。所以,我试图通过使用LayoutParams来设置FAB的宽度和高度,但setLayoutParams不起作用。我尝试使用ImageButton,因为FAB扩展了ImageButton并且它适用于ImageButton。有人能告诉我为什么会这样吗?
final GridLayout.LayoutParams lp = (GridLayout.LayoutParams) mFab1.getLayoutParams();
ViewTreeObserver vto = mFab2.getViewTreeObserver();
ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
lp.height = mFab2.getHeight();//Height is ready
lp.width = mFab2.getWidth(); //Width is ready
Log.d(LOG_TAG, (String.valueOf(mFab2.getWidth())) + " and " + (String.valueOf(mFab2.getHeight())));
}
} ;
vto.addOnGlobalLayoutListener(listener);
mFab1.setLayoutParams(lp);// Works here if mFab1 is ImageButton
mFab1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d(LOG_TAG, "Outside "+(String.valueOf(mFab1.getWidth())) + " and " + (String.valueOf(mFab1.getHeight())));
if(mFab1.getWidth()==0){
mFab1.setLayoutParams(lp);//trying to set LayoutParams again
Log.d(LOG_TAG, "Inside "+(String.valueOf(mFab1.getWidth())) + " and " + (String.valueOf(mFab1.getHeight())));
}
}
});
我使用v7 GridLayout并且在xml中mFab1的高度和宽度都设置为零,因为我使用权重。由于列跨度为2,layout_gravity设置为" center_horizontal"因此,将宽度设置为零将不起作用,这就是为什么我想在java代码中设置宽度,它对ImageButton非常有效。
<android.support.v7.widget.GridLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:columnCount="2"
app:rowCount="6"
tools:context=".fragment.MainNavigationActivityFragment"
>
<ImageButton
android:id="@+id/mFab2"
app:layout_rowWeight="1"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"/>
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab3"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button1"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/transparent"
android:textColor="@color/colorPrimary"
android:fontFamily="roboto-condensed"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button2"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab3"
android:textColor="#FFFFFF"
android:textSize="30sp"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab4"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button3"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/transparent"
android:textColor="@color/colorPrimary"
android:fontFamily="roboto-condensed"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button4"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<android.support.design.widget.FloatingActionButton
app:layout_rowSpan="1"
app:layout_rowWeight="1"
android:id="@+id/mFab1"
app:layout_columnSpan="2"
app:layout_gravity="center_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:backgroundTint="@color/colorPrimary"
android:src="@drawable/icon"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
/>
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="center_horizontal"
app:layout_columnSpan="2"
app:layout_columnWeight="1"
android:id="@+id/button5"
android:text="Some Text"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" />
</android.support.v7.widget.GridLayout>
答案 0 :(得分:0)
我想我能理解你想做什么。
您似乎想根据mFab2的高度/宽度设置mFab1高度/宽度。是吗?
我认为你只需要为mFab2添加一个监听器。因此,当读取mFab2的高度/宽度时,将它们应用于mFab1。您不需要为mFab1设置监听器。
请你试试下面的代码:
findViewById(R.id.mFab2).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View mFab1 = findViewById(R.id.mFab1);
View mFab2 = findViewById(R.id.mFab2);
mFab1.getLayoutParams().height = mFab2.getHeight();
mFab1.getLayoutParams().width = mFab2.getWidth();
Log.d(LOG_TAG, (String.valueOf(mFab2.getWidth())) + " and " + (String.valueOf(mFab2.getHeight())));
}
});
您可以使用此代码替换您在说明中共享的所有代码。 然后,让我知道是否有效。
当然,如果该代码有效,您可以添加适当的空检查和任何其他错误检查