情景:I've four buttons arranged Relative Layout。这些按钮的文本因应用程序生命而异,使按钮按文本长度缩小或缩放。使用: RelativeLayout.getChildAt()。setText();
Q1)但我要求每个按钮占据屏幕宽度的40%,但高度不同。 Q2)特别是我的代码需要使用 getChildAt() 来访问按钮。
我应该使用什么布局类型来替换RelativeLayout以将Button的宽度设置为屏幕宽度的40%,特别是这样我可以使用 getChildAt() 来访问这些按钮? SomeLayout.getChildAt(); ,以便布局是按钮的直接父级。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
Log.d(TAG,"Fb initialzed");
callbackManager = CallbackManager.Factory.create();
//LoginButton loginButton= (LoginButton)findViewById(R.id.fb_login_button);
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult result) {
// TODO Auto-generated method stub
Log.d(TAG,"Login success");
Intent I = new Intent(getApplicationContext(), Games.class);
startActivity(I);
}
private void showAlert() {
new AlertDialog.Builder(GameSurvey.this)
.setTitle(R.string.cancelled)
.setMessage(R.string.permission_not_granted)
.setPositiveButton(R.string.ok, null)
.show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.d(TAG,"Login cancelled");
}
@Override
public void onError(FacebookException error) {
// TODO Auto-generated method stub
if(error instanceof FacebookAuthorizationException)
Log.d(TAG,error.toString());
showAlert();
}
});
Log.d(TAG, "here");
setContentView(R.layout.activity_game_survey);
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
updateUI();
// It's possible that we were waiting for Profile to be populated in order to
// post a status update.
}
};
profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
}
答案 0 :(得分:1)
你其实很幸运。 Android刚刚发布了一个新的百分比支持库。他们还没有文档,但这是一个很好的示例项目,您可以使用它来了解如何使用库。它基本上允许您按屏幕百分比调整视图小部件的大小。
https://github.com/JulienGenoud/android-percent-support-lib-sample
这里有两篇文章也在谈论它:
http://www.androidauthority.com/using-the-android-percent-support-library-630715/
http://inthecheesefactory.com/blog/know-percent-support-library/en
答案 1 :(得分:1)
Android百分比支持库通过替换我们传统的LinearLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fifty_huntv"
android:background="#ff7acfff"
android:text="20% - 50%"
android:textColor="@android:color/white"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:layout_toRightOf="@id/fifty_huntv"
android:background="#ffff5566"
android:text="80%-50%"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="80%"
app:layout_widthPercent="50%"
/>
</android.support.percent.PercentRelativeLayout>
太棒了!!!
答案 2 :(得分:0)
现在我想不出任何允许你这样做的布局元素。
您可以在代码onCreate
或其中一个init视图/变量方法上尝试此操作,获取按钮并将大小设置为屏幕的40%,如:
Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
RelativeLayout rlParent = findViewById(R.id.<RELATIVE_LAYOUT_ID>);
for(int i = 0; i < 4; i++)
{
View btn = rlParent.getChildAt(i);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)btn.getLayoutParams();
params.width= screenSize.y * 0.4f;//40%
btn.setLayoutParams(params);
}