XML 代码中的布局属性在使用 addView 方法时被忽略
Layout attributes in XML code those are ignored when using addView method
我是韩国人,下图是韩语。好吧.. 这个事实并不重要。我尝试添加自定义视图,在线性布局中,并且成功地在线性布局中。但它不在中心水平线上。我确实确实将 linearlayout 中心的重力设置为水平。但这是行不通的。我想问题的原因是布局参数。自定义视图 class 没有问题,并且调试时没有出现任何错误。我该怎么办??
Activity 在 XML 代码中:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling">
<LinearLayout
android:id="@+id/select_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<!--Here is-->
</LinearLayout>
</ScrollView>
添加视图方法:
ThemeManager mTheme;
Button selectTopBoardButton;
LinearLayout selectBody;
ThemeView c0001;
ThemeView c0002;
ThemeView c0003;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());
NetworkDetector networkDetector = new NetworkDetector(this);
if (!networkDetector.isConnected()) {
Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
}
selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
selectBody = (LinearLayout) findViewById(R.id.select_body);
mTheme = new ThemeManager(this);
c0001 = mTheme.getThemeView("c0001");
c0002 = mTheme.getThemeView("c0002");
c0003 = mTheme.getThemeView("c0003");
selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = getResources().getString(R.string.main_request_btn06_url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
selectBody.addView(c0001);
}
尝试像这样为自定义视图设置重力
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView
我认为你的子视图 layout_width="match_parent" 请试试这个代码
c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
selectBody.addView(c0001);
我是韩国人,下图是韩语。好吧.. 这个事实并不重要。我尝试添加自定义视图,在线性布局中,并且成功地在线性布局中。但它不在中心水平线上。我确实确实将 linearlayout 中心的重力设置为水平。但这是行不通的。我想问题的原因是布局参数。自定义视图 class 没有问题,并且调试时没有出现任何错误。我该怎么办??
Activity 在 XML 代码中:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling">
<LinearLayout
android:id="@+id/select_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<!--Here is-->
</LinearLayout>
</ScrollView>
添加视图方法:
ThemeManager mTheme;
Button selectTopBoardButton;
LinearLayout selectBody;
ThemeView c0001;
ThemeView c0002;
ThemeView c0003;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());
NetworkDetector networkDetector = new NetworkDetector(this);
if (!networkDetector.isConnected()) {
Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
}
selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
selectBody = (LinearLayout) findViewById(R.id.select_body);
mTheme = new ThemeManager(this);
c0001 = mTheme.getThemeView("c0001");
c0002 = mTheme.getThemeView("c0002");
c0003 = mTheme.getThemeView("c0003");
selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = getResources().getString(R.string.main_request_btn06_url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
selectBody.addView(c0001);
}
尝试像这样为自定义视图设置重力
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView
我认为你的子视图 layout_width="match_parent" 请试试这个代码
c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
selectBody.addView(c0001);